← Back to challenges

Cumulative Array Sum

JavaScriptHardarraysmathnumbersloops

Instructions

Create a function that takes an array of numbers and returns an array where each number is the sum of itself + all previous numbers in the array.

Examples

cumulativeSum([1, 2, 3]) ➞ [1, 3, 6]

cumulativeSum([1, -2, 3]) ➞ [1, -1, 2]

cumulativeSum([3, 3, -2, 408, 3, 3]) ➞ [3, 6, 4, 412, 415, 418]

Notes

Return an empty array if the input is an empty array.

javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.
Next: Rotated Words