Neat solution using the rest parameter syntax from ES6.
function addTogether(x, ...y) {
if (typeof(x) == 'number') {
if (y.length == 0) {
// if one argument is passed
return function(y) {
return typeof(y) == 'number' ? x + y : undefined;
};
} else {
// if two arguments or more are passed
return (typeof(y[0]) == 'number') ? x + y[0] : undefined;
}
} else {
return undefined;
}
}
Challenge: Arguments Optional
Link to the challenge: