Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to add all the items in a list together?

Asked by 3 years ago
Edited 3 years ago

I am still working with lists/arrays and i have a question, i want to add all the items in the list what i mean is if i have an array like so: {3,2,3,1,3} the output should be 12 how would i do this?

0
*I don't have any code for this Retallack445 75 — 3y

2 answers

Log in to vote
0
Answered by
0hsa 193
3 years ago
Edited 3 years ago

this should workd:

total = 0
array = {3,2,3,1,3}
for _,n in ipairs(array) do
    total+=n
end
Ad
Log in to vote
1
Answered by 3 years ago

This works for me when i have to get the sum


Add = {1, 2, 3, 4} local sum = 0 table.foreach(Add, function(i, v) sum = sum + v end) print(sum)

Answer this question