mirror of
https://bitbucket.org/Mattrixwv/luaclasses.git
synced 2025-12-06 18:33:59 -05:00
Added getSum and getProd for tables
This commit is contained in:
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user