Sum function parameter and invoke in Javascript

Create sum function with parameter

There we won’t know how much parameter will be there ?

function sum(x){
  if(arguments.length == 1){
    return arguments[0]
  } else if(arguments.length == 2){
    return arguments[0] + arguments[1];
  } else if(arguments.length == 3){
    return arguments[0] + arguments[1] + arguments[2] 
  }
}

//1st Time
console.log(sum(5));
//2nd Time
console.log(sum(5,7));
//3rd Time
console.log(sum(5,7,8));

Now With two Invoke

function sum(x){
  return function(y){
    return x + y
  }
}

console.log(sum(5)(7));

Leave a Reply

Your email address will not be published. Required fields are marked *