Facebook Pixel
Implement sum(1)(2)(3)(4)..(n) in JavaScript

Is sum(1)(2)(3) an Amazon interview question?

The answer

Yes. Amazon asks candidates to implement a curried sum function that chains indefinitely. It tests understanding of currying, closures, function chaining, and JavaScript type coercion (if using toString).

More FAQs in Implement sum(1)(2)(3)(4)..(n) in JavaScript

function sum(a) { return function(b) { if (b !== undefined) return sum(a + b); return a; }; }. Each call returns a function that accumulates. Call with no argument to get the result: sum(1)(2)(3)().

Two approaches: (1) call with no argument: sum(1)(2)(3)() - check if b !== undefined. (2) toString override: sum(1)(2)(3) + 0 - JavaScript calls toString when arithmetic is performed.

Currying and closures. Each call returns a function that closes over the accumulated sum. Without closures, the inner function would not have access to the previous arguments.

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.