I have the below code wherein, I have wrapped functions to achieve the above behaviour. But unfortunately it returns expected result only when the no of parameters equals to 2.
function baseCurry (func) {
return function (...args) {
if (args.length >= func.length) {
return func.call(this, ...args)
} else {
return baseCurry(func.bind(this, ...args))
}
}
}
function addHelper (x, y) {
return x + y;
}
const superCurry = baseCurry(addHelper);
Test Cases:
console.log(superCurry(1, 5)); // 6
console.log(superCurry(1)(5)); // 6
console.log(superCurry(1)); // [Function (anonymous)]
console.log(superCurry(1)(2)(3)); // Error
console.log(superCurry(1,2)(3)); // Error
I need to change it in such a way that it gives expected result for all n >= 1
, where 'n' is the number of parameters
Note:
The params can be passed in any combinations like
console.log(superCurry(1,2)(3)(4))
console.log(superCurry(1,2,3)(5,7)(4)(8,9))
Thanks in advance