-6

I am learning javascript.I stick to one problem. I want same function to generate same result for this code:

Multiply(5, 2); // return 10
Multiply(5)(2); // return 10
Andrew1221
  • 11
  • 4

1 Answers1

4

It would be what you want.

function Multiply(a, b){
  if(b != null){
    return a*b;
  }
  return function(c){
    return a*c;
  }
}

console.log(Multiply(5, 2));
console.log(Multiply(5)(2));
sjahan
  • 5,720
  • 3
  • 19
  • 42
sapics
  • 1,104
  • 8
  • 22