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
To have an unlimited callchain you need some js trick:
The problem of such variable currying is that its infinite, so theres ( usually ) no way to end it.
However in javascript its possible to assign methods to functions so we can easily somewhen call the method instead of the function to end the chain:
So you can simply do:
The + is equal to valueOf and is therefore neccessary to end the chain, however this call to valueOf is inferred by many operations in javascript ( all math operations like - * /).