Facebook Pixel
Currying Interview Questions in JavaScript

How do you implement sum(1)(2)(3)() in JavaScript?

The answer

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.

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

Want to upskill yourself?

Your learning library

Small steps. Big possibilities.

Find your next course. Build something great.

Our courses are taking a Coffee break, but your curiosity shouldn't. Stay engaged with namastedev linkedin, youtube, discord and other resources while you wait.

0

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.