Question : lua calculate average number
Answered by : oldfashioned-oyster-itgli4pfpbg5
function math.average(t) local sum = 0 for _,v in pairs(t) do -- Get the sum of all numbers in t sum = sum + v end return sum / #t
end
print(math.average({9, 10, 12, 13, 13, 13, 15, 15, 16, 16, 18, 22, 23, 24, 24, 25})) -- Result: 16.75
Source : | Last Update : Sat, 21 Nov 20