Memoize
Cache a function by its arguments so the same call never computes twice.
JavaScript · ES6 — Pratik Rai ·
sum(1)(2)(3) with no fixed arity — the running total appears when the result is coerced.
Write sum(n) so it can be called any number of times in a chain, each call adding to a running total.
The total appears when the result is converted to a primitive — by +, by String(), or inside a template literal. Two separate chains must not share a total.
Input:
JSfile.javascript1+sum(1)(2)(3)
Output:
6
Each call adds to the closure's total; the unary + asks the returned function for its primitive value.
Goal: Chain indefinitely, and produce the total on coercion.
Continue learning with these related challenges
Cache a function by its arguments so the same call never computes twice.
JavaScript · ES6 — Pratik Rai ·
Test your JavaScript skills by predicting console output for tricky code snippets. Covers hoisting, closures, this binding, async operations, and event loop quiz questions.
JavaScript · ES6 — Pratik Rai ·
Level up your JavaScript expertise with advanced tips, tricks, and gotchas. Master closures, hoisting, coercion, and event loop behavior to write cleaner, bug-free code.
JavaScript · ES6 — Pratik Rai ·
Cache a function by its arguments so the same call never computes twice.
JavaScript · ES6
Pratik Rai ·
Test your JavaScript skills by predicting console output for tricky code snippets. Covers hoisting, closures, this binding, async operations, and event loop quiz questions.
JavaScript · ES6
Pratik Rai ·
Level up your JavaScript expertise with advanced tips, tricks, and gotchas. Master closures, hoisting, coercion, and event loop behavior to write cleaner, bug-free code.
JavaScript · ES6
Pratik Rai ·