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
  • 572,313
  • 128
  • 898
  • 1,281
Zack
  • 621
  • 2
  • 11
  • 22

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
  • 72,564
  • 26
  • 147
  • 178