0

Can Anybody help me to write this function Write a function where the output for both “sum(2,3)” and “sum(2)(3)” will be 5

I think we need to write function using closer!

Neelam
  • 11

1 Answers1

3

Syntax as follows: sum(2)(3) is related with currying. It means that you need to return another function inside the main function to get the desired result.

Here's a nice video about currying on Youtube, from which I've learned a lot.

function sum(a){
  if (arguments.length == 1) {
    return function(b){
      return a + b;
    }
  } else {
    return Object.keys(arguments).reduce((a,b) => arguments[a] + arguments[b]);
  }
}

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

As Karl-André Gagnon proposed, it can be also done with bind function.

function sum(a){
  if (arguments.length == 1) {
    return sum.bind(this, a)
  } else {
    return Object.keys(arguments).reduce((a,b) => arguments[a] + arguments[b]);
  }
}

console.log(sum(2,3));
console.log(sum(2)(3));
kind user
  • 40,029
  • 7
  • 67
  • 77
  • it this closure function! – Neelam Mar 22 '17 at 13:23
  • Thinking out of the box here, and better readability IMHO, you could return `return sum.bind( this, a );` instead of a new anonymous function (even if `bind` technically return an anonymous function). – Karl-André Gagnon Mar 22 '17 at 13:23
  • @Karl-AndréGagnon Would you like to tell me how does it exactly work? That `bind` function. `this` keyword refers to the first arg (2) and `a` refers to the second one (3)? – kind user Mar 22 '17 at 13:31
  • Hi Guys, Can you please explain me this function! function sum(a){ if (arguments.length == 1) { return sum.bind(this, a) } else { console.log(Object.keys(arguments).reduce((a,b) => arguments[a] + arguments[b])); // I dont understand this line. } } – Neelam Mar 22 '17 at 13:35
  • Or can we achieve this using closure function! – Neelam Mar 22 '17 at 13:38
  • @Neelam Yes, we can. Using `bind` function is just another way how to solve it. – kind user Mar 22 '17 at 13:39
  • 1
    @Kinduser When using bind, it return a new function. The first argument of `.bind` is the context (value of `this`). In this case, it could be anything since `sum()` is not using `this`. Just a good practice to use the same this as the callee on recursive function. Every other arguments of `.bind()` will be the future arguments of the bound function when called. Then, arguments passed when calling the bound function will be **after** the argument passed when calling `.bind()`. So `3 === arguments[1]`. Was that clear enough? ;) Kinda hard to explain a native function... – Karl-André Gagnon Mar 22 '17 at 13:41
  • @Karl-AndréGagnon Thank you. I appreciate it (: – kind user Mar 22 '17 at 13:45
  • @Karl-AndréGagnon *So 3 === arguments[1]* - shouldn't be it `arguments[0]`? – kind user Mar 22 '17 at 13:56
  • 1
    @Kinduser Nope, `arguments[0] === 2` in this case since the first argument is the second argument of `.bind()`. if bind had 3 arguments, then `3` would be the third argument. Take a look at this, it might be clearer than the example above: https://jsfiddle.net/d3go9vcu/ – Karl-André Gagnon Mar 22 '17 at 14:20