0

The simplest question for that is like to write a function that could return the sum of all the parameters. How I can do that?

function add () {

}
add(1)(2)(3)();  //6
add(5)(6)(7)(8)(9)()  //35
leonlong
  • 57
  • 1
  • 5
  • 2
    Where is your effort? – Rikin Nov 16 '17 at 21:21
  • 1
    Possible duplicate of [JavaScript variable number of arguments to function](https://stackoverflow.com/questions/2141520/javascript-variable-number-of-arguments-to-function) – matmo Nov 16 '17 at 21:22
  • Please attempt to write this function. I would suggest changing "Unlimited parameters" to an array based on what may be your skill level. – geeves Nov 16 '17 at 21:23
  • That's not a function returning the sum of "all the parameters", there's a single parameter, and you're calling `add` once, and calling some other function afterwards. – Dave Newton Nov 16 '17 at 21:26
  • @matmo No, this is not a function taking multiple parameters. – Dave Newton Nov 16 '17 at 21:26
  • 1
    A function that returns the sum of all the paramaters would be called like `add(1, 2, 3)`. – Barmar Nov 16 '17 at 21:30
  • https://stackoverflow.com/questions/47286900/identify-how-the-function-has-been-called-in-closure-javascript/47287290#47287290 – Jonas Wilms Nov 16 '17 at 21:30

3 Answers3

5

I think this is exactly what you need:

function add(value) {
   return (val) => val !== undefined ? add(value + val) : value;
}

console.log(add(2)(2)()); //4
console.log(add(2)(2)(5)(5)()); //14
console.log(add(1)(1)(1)(1)(1)()); //5
console.log(add(1)(1)(0)(1)(1)()); //4

How it works

For every call it declares a function inside, in result it creates a closure(persistent scope) in every call. Function created in that way has access to its parameter + previous call parameter due to existing closure.

So if I call add(2)(3)():

  • add(2) - returns function with visible 2 value
  • add(2)(3) - calls second function with input 2 + 3 and return third function with visible value equal 5
  • add(2)(3)() - ends computation due to empty param and returns the value

To finish the computation pipe the last call needs to be without a value.

Maciej Sikora
  • 19,374
  • 4
  • 49
  • 50
3

The basic idea is to create a closure with a variable sum that we can update, and return the sum if the value is undefined, or the the inner function:

const add = (n) => {
  let sum;
  
  const inner = (n) => n === undefined ? sum : (sum = (sum || 0) + n, inner);
  
  return inner(n);
};

console.log(add(1)(2)(3)());  //6
console.log(add(5)(6)(7)(8)(9)());  //35
Ori Drori
  • 183,571
  • 29
  • 224
  • 209
  • IMO questions like this are best left as an exercise for the asker, potentially providing hints to how to go about solving them. Teach a man to fish and all that. – Dave Newton Nov 16 '17 at 21:33
  • @DaveNewton - I know, but I'm not really concerned about the OP. Just wanted to see other answers. – Ori Drori Nov 16 '17 at 21:42
  • AFAICT that doesn't require posting an answer, but ok. – Dave Newton Nov 16 '17 at 21:44
  • @DaveNewton it is not true that such approach is fully wrong. It is close to currying and enables to use functions with encapsulated values over the application instead of variables. You thread function as first class citizen instead of variable with pure value. – Maciej Sikora Nov 16 '17 at 22:02
  • @MaciejSikora I don't think it's wrong at all, I'm just opposed to answering questions that don't show any effort. The point of questions like the OP was asked is either to (a) gauge knowledge, or (b) make them think. Handing over answers defeats the purpose on both counts. YMMV. – Dave Newton Nov 16 '17 at 22:24
0

I would just use a Spread syntax like this:

function add(...values) {
  return values.reduce((sum, value) => sum + value, 0)
}

console.log(add(1, 4, 34, 45, 3, 4, 5))
Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338