Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Private Bank Balance

A Closer Look at Functionshard

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

Implement `bankOps(ops)` where `ops` is an array of amounts (positive for deposits, negative for withdrawals). Track a balance in a closure, ignore any withdrawal that would make the balance negative, and return the final balance starting from `0`.

Sample tests

Input: bankOps([100,-30,-50])
Output: 20
Input: bankOps([100,-200])
Output: 100

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Allowing a withdrawal that drives the balance negative.
  • Exposing balance directly instead of through the closure.

Learning resources

  • MDN: Closures
Visualize this concept: Closures →
Approach & explanation (try first)

The balance is captured privately; each operation is applied only if it keeps the balance non-negative, modeling encapsulated state.

Loading...
⌘/Ctrl + Enter

Run your code to see results.