I’m having trouble currying a function if only one argument is passed to the function. I’m returning a reference to another function when a second argument is passed.
If the function invocation is changed to addTogether(2)(3)
, I get the following error: TypeError: addTogether(...) is not a function
.
Your code so far
function addTogether() {
function valid_arg(value) {
return Number.isInteger(value) ? value : undefined;
}
var args = Array.prototype.slice.call(arguments);
if (args.every(valid_arg) && args.length === 2) {
return arguments[0] + arguments[1];
} else {
return undefined;
}
function add(y) {
if (valid_arg(y)) {
return arguments[0] + y;
}
return undefined;
}
return add;
}
addTogether(2, 3);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:77.0) Gecko/20100101 Firefox/77.0
.
Challenge: Arguments Optional
Link to the challenge: