Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Sum with Reduce

Working With Arrayseasy

Course · Section 11: Working With Arrays · Lecture 156: Data Transformations: map, filter, reduce

Implement `sumAll(nums)` returning the sum, using `reduce`.

Sample tests

Input: sumAll([1,2,3,4])
Output: 10
Input: sumAll([])
Output: 0

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Omitting the initial value, which breaks on an empty array.

Learning resources

  • MDN: Array.prototype.reduce
Visualize this concept: map, filter, and reduce →
Approach & explanation (try first)

reduce with an initial 0 sums the array, working even when empty.

Loading...
⌘/Ctrl + Enter

Run your code to see results.