Added getSum and getProd for tables

This commit is contained in:
2019-03-22 19:29:53 -04:00
parent b0ba590c53
commit 3d6f2e2da9

View File

@@ -175,3 +175,23 @@ function getDivisors(goalNumber)
--Return the list --Return the list
return divisors; return divisors;
end end
function getSum(ary)
sum = 0; --Holds the sum of all elements. Start at 0 because num+1 = num
--Look through every element in the array and add the number to the running sum
for location = 1, #ary do
sum = sum + ary[location];
end
--Return the sum of all elements
return sum;
end
function getProd(ary)
prod = 1; --Holds the product of all elements. Start at 1 because num*1 = num
--Look through every element in the array and add the number to the running product
for location = 1, #ary do
prod = prod * ary[location];
end
--Return the product of all elements
return prod;
end