0
function makeMultiplier(x){
        return function(y) {
            return x * y;
    }
}

var by10 = makeMultiplier(10);
console.log(by10(7));

How is it possible to pass in two parameters when make multiplier only accepts one? I'm unsure as to how this syntax is working.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Zack
  • 661
  • 2
  • 11
  • 27

1 Answers1

5

How is it possible to pass in two parameters when make multiplier only accepts one?

Because makeMultiplier() returns a function.


function makeMultiplier(x){
    return function(y) {
        return x * y;
    }
}

var by10 = makeMultiplier(10); // by10 is now function (y) { return x * y }, with x bound to 10.
console.log(by10(7)); // So now we can call it like a function.

I've coincidentally answered this question as well today about functions returning functions. It might help.

Community
  • 1
  • 1
Matt
  • 74,352
  • 26
  • 153
  • 180