Total of a list
A JavaScript exercise from Scriptdrill. Solve it with a code editor in your browser that runs your function against example and hidden tests, and it is checked the moment you submit.
The task
Return the total of an array of numbers. An empty array totals `0`. Numbers can be negative or have decimals.
What this exercise teaches
reduce walks the array once and carries a running total. Passing 0 as the starting value matters: without it, reduce on an empty array throws a TypeError.
A hint if you get stuck
`reduce` can carry a running total through the array. Give it a starting value of `0`, and the empty array is handled for free.
Try it now, no account needed
This exercise is free. Open it on the practice page, solve it, and get instant feedback with a hint and a worked solution after two tries.
Open this exercise