How do you implement sum(1)(2)(3)() in JavaScript?
function sum(a) { return function(b) { if (b !== undefined) return sum(a + b); return a; }; }. Call with no argument to terminate and return the accumulated sum.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in Currying Interview Questions in JavaScript
Same pattern: function sum(a) { return function(b) { if (b !== undefined) return sum(a + b); return a; }; }. Or use toString: function sum(n) { const fn = m => sum(n + m); fn.toString = () => n; return fn; }.
function curry(fn) { return function curried(...args) { if (args.length >= fn.length) return fn(...args); return (...next) => curried(...args, ...next); }; }.
Currying transforms f(a, b, c) into f(a)(b)(c) - one argument at a time. Partial application presets some arguments and returns a function for the rest: f'(b, c) with a preset.
Still have questions?
Browse all our FAQs or reach out to our support team
