Home
← All questions

Infinite currying — add(1)(2)(3)(4) in JavaScript

MediumPremiumJavaScript

JavaScript interview practice: Implement add(1)(2)(3)(4) that sums numbers via chained calls.

Problem

Implement add so you can chain calls like add(1)(2)(3)(4) and get the total sum.

Support both ways to finish the chain:

  1. Unary plus coercion: +add(1)(2)(3)(4) → 10
  2. Empty call: add(1)(2)(3)() → 6

Each call adds its argument to a running total and returns a function that accepts the next number.

Input

+add(1)(2)(3)(4);
add(1)(2)(3)();

Expected output

  • First expression: 10
  • Second expression: 6

Implement:

function add(a) {
  // Your code here
}
Premium question

Unlock the in-browser editor, test cases, and model solution with Mentoxis Premium.

Unlock with Premium