Memoize a Function
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.
A normal curry knows when to stop: it compares collected arguments against fn.length. An infinite chain has no such signal. Nothing in sum(1)(2)(3) says the chain has ended.
The answer is that the chain never ends — it just gets asked for a value. Each call returns the same function with a larger total, and coercion is what finally reads it.
JSfile.javascript1function sum(first) { 2 let total = first; 3 const next = (value) => { total += value; return next; }; 4 next.valueOf = () => total; 5 next.toString = () => String(total); 6 return next; 7}
valueOf handles numeric contexts (+sum(1)(2), arithmetic) and toString handles string ones (template literals, String()). Define only one and the other falls back to "[object Object]", which is exactly the bug the reported candidate hit.
Keeping total inside the outer sum call rather than in module scope is what stops two chains sharing a total — a shared accumulator means the second sum(...) continues the first.
Goal: Chain indefinitely, and produce the total on coercion.
+sum(1)(2) triggers it, which is how a function that always returns another function can still evaluate to 3.String() — ask for a string, not a number. Define only valueOf and those contexts fall back to "[object Object]", which is the most common mistake on this problem.fn.length. An infinite chain has no arity to compare against, so coercion replaces the arity check entirely.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 ·