0

This is a simplified version with addition of 2 numbers:

const sum = (a) => {
  return function(b) {
    return a + b;
  }
}

console.log(
  sum(2)(2)
);

This is a more difficult option.

I couldn't find this problem on google, but I'm just wondering how to solve this. I couldn't think of a solution.

const sum = (a) => {
  // How to make it?
}

console.log(
  sum(2)(2)(4)() // Expected result: 8
  sum(2)(2)(4)(4)(4)() // Expected result: 16
  sum(2)(2)(4)(10)(20)(1)() // Expected result: 39
);
  • "The number of arguments can be infinite ". That means the inner function should be able to accept variable number of arguments – Phalgun Feb 02 '22 at 03:21
  • var sum = (a) => { return function (b) { if (b) { return sum(a + b); } else { return a + 0; } }; }; – Arun Feb 02 '22 at 04:27

0 Answers0