HomePremium · ₹1199
← All questions

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

Medium
Was this asked in an interview?

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
}
Infinite currying — add(1)(2)(3)(4) — JavaScript Interview Question | Mentoxis