Use rest parameters: function sum(...args) { const total = args.reduce((a, b) => a + b, 0); return function(b) { if (b !== undefined) return sum(total + b); return total; }; }.
How do you support sum(1, 2)(3) with multiple arguments?
More FAQs in sum(1)(2)(3) Variations and Follow-Up Questions
function sum(n) { const fn = m => sum(n + m); fn.toString = () => n; return fn; }. When JavaScript needs a primitive (like +0 or console.log), it calls toString and returns the accumulated sum.
How would you support multiple arguments (rest params), add other operations (multiply, subtract), make it a generic curry function, and what is the time/space complexity.
Yes. The chain can continue indefinitely: sum(1)(2)(3)(4)(5)...(n). It only terminates when you call with no argument (or when JavaScript calls toString for type coercion). Each call creates a new function.
Still have questions?
Browse all our FAQs or reach out to our support team
Want to upskill yourself?
Your learning library
Small steps. Big possibilities.
Find your next course. Build something great.