Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Running Totals

A Closer Look at Functionsmedium

Course · Section 10: A Closer Look at Functions · Lecture 144: Closures

Implement `accumulate(nums)` using a closure that keeps a running sum, returning the array of running totals.

Sample tests

Input: accumulate([1,2,3])
Output: [1,3,6]
Input: accumulate([])
Output: []

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Declaring the sum inside the mapper resets it every call.

Learning resources

  • MDN: Closures
Approach & explanation (try first)

A closure over sum lets each mapped call add to and return the accumulating total.

Loading...
⌘/Ctrl + Enter

Run your code to see results.