Recently i faced one problem in hackerrank which has to calculate multiplication operation and has to return the answer. For example
function multiply(a,b) {
return a*b;
}
Now here is the problem the function might call in different ways such as
multiply(4,5);
multiply(4)(5);
multiply(4)(5)(6);
I know we have to closure apporach for the second one which is multiply(4)(5). I had written code for that
function multiply(a,b) {
return function(b) {
return a*b;
}
}
Now what if its been multiply function has been called with 3 arguments multiply(4)(5)(6). How can i identify how the function has been called and how can i write a common solution for all the inputs.
Any help is appreciated. Thanks