3

I have a simple demo:

function add(a) {
    return function (b) {
        return a + b
    }
}

Now when I call add(1)(2) it will give me 3

but I want something like this:

add(1)(2)(3) -> 6

add(1)(2)(3)(4) -> 10

it means I can add more function if I want. How to write it?

2

3 Answers 3

7

I have seen a similar problem elsewhere, but there the requirement was slightly different. Instead of having add(1)(2)(3) return 6, it was required to have add(1)(2)(3)() return 6.

Notice that the last invocation of the function does not have any arguments. This is important distinction because the function should either return another function that taken in a number as an argument or it should return the total of the arguments passed so far. Without the distinction of the last parameter being undefined (or NaN) it is not possible to decide whether to return a currying function or the total so far.


So I will assume that what you want is to have add(1)(2)(3)() return 6. Here's how you could do that:

  • As already explained, the add function either returns another function that takes an argument if the current parameter is a number.

  • If the current argument is undefined (or NaN) it returns the sum.

  • You keep track of the sum using a variable sum that is available to the total function through closure.

function add(x) {
  var sum = x;
  return function total(y) {
    if (!isNaN(y)) {  
      sum += y;
      return total;
    } else {
      return sum;
    }
  }
}

console.log(add(5)(2)());
console.log(add(5)(3)());
console.log(add(1)(2)(3)(4)());

Sign up to request clarification or add additional context in comments.

Comments

0

Create one array and pass to function and execute same function recursively

something like this

var arr = [1,2,3,4];

function add(a , i, sum) {
    if(i >= a.length ){ 
      return sum;
    } else {
      sum = sum + a[i];
      ++i;
      return add(a, i, sum)
    }
}
var b = add(arr ,i=0,sum = 0);
console.log(b)

1 Comment

My question is function return function, not like your :)
0

If you want to work with array. Only if someone googles for it since Nisargs answer is absolutely fine.

function add(a) {
    arrInt = a;
    return function total(arrInta) {
        b = 0;
        arrInt.forEach(element => {
            b = b + element;
        });
        return b;
     };
 }

 a = [1,2,3]

 console.log( add(a)());
 a = [1,2,3, 4]
 console.log( add(a)());

Which returns:

​​​6​​​​​ at ​​​add(a)​​​ ​quokka.js:14:1​

​​​​​10​​​​​ at ​​​add(a)​​​ ​quokka.js:16:1​

2 Comments

My question is function return function, not like your :)
@Khuong Than like this, but like I said only for help searching users :D Now its function return function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.