You need Login/Signup to run/submit the code.
Implement Currying | JavaScript Interview Questions
@Yomesh Gupta
The concept of currying happens when a function does not accept all of its arguments up front. Instead it takes the first argument and returns another function. The returned function is supposed to be called with the second argument, which in turn again returns another function. And this continues till all the arguments have been provided. And then the function at the end of the chain will be the one that returns the value that we want.
const add = (a, b, c, ..., n) => {
return a + b + c + ... + n;
};
const curriedAdd = curry(add);
curriedAdd(1,2,3,4,5); // returns 15;
curriedAdd(1)(2,3,4,5); // returns 15;
curriedAdd(1,2)(3,4,5); // returns 15;
curriedAdd(1,2,3,4)(5); // returns 15;
Points to Remember
- Input function to
curry
can taken
arguments. curriedAdd
should return the final output when received arguments matches the originaladd
function arguments length.