#Closures#Currying

Infinite Curried Sum

sum(1)(2)(3) with no fixed arity — the running total appears when the result is coerced.

By Pratik RaiMedium

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.

Examples

Input:

JSfile.javascript
1+sum(1)(2)(3)

Output:

6

Each call adds to the closure's total; the unary + asks the returned function for its primitive value.

Constraints

  • Every argument is a number
  • Chains can be arbitrarily long

Notes

  • Unlike a fixed-arity curry, nothing tells the function when the chain has ended — coercion is the only signal.
  • A function is an object, and objects choose how they coerce.

How to solve it

Currying with no arity to stop at

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.

Approach

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.

Implementation

JSfile.javascript
1function 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}

Worth knowing

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.

Frequently asked questions

How can a curried sum have no fixed number of calls?
Each call returns the same function with a larger running total, so the chain can go on indefinitely. Nothing signals the end — the total is read when the result is converted to a primitive.
What does valueOf do here?
It tells JavaScript what number this object becomes in a numeric context. +sum(1)(2) triggers it, which is how a function that always returns another function can still evaluate to 3.
Why define toString as well?
Because string contexts — template literals, 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.
How is this different from a normal curry?
A normal curry knows when to stop by comparing collected arguments against fn.length. An infinite chain has no arity to compare against, so coercion replaces the arity check entirely.

Related Challenges

Continue learning with these related challenges

View All
JavaScript

Memoize a Function

Cache a function by its arguments so the same call never computes twice.

JavaScript · ES6 — Pratik Rai ·

JavaScript

JS Output Challenges

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 ·

JavaScript

Get smart in javascript

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 ·