Hello everyone.
I wrote the following code to solve the challenge Arguments Optional and it fails on the testcase with the array inside it. I really have no idea why, because it absolutely checks if all arguments are a number! As I misread the instructions slightly, I wrote the function so that it should cope with any number of arguments, not just two - but that seems not to be the problem here.
function addTogether() {
const args = [].slice.call(arguments);
console.log("INPUT: " + args);
if (args.length > 1) {
if (args.every(val => typeof(val) === "number")) {
return args.reduce((sum, val) => sum += val);
} else {
return undefined;
}
} else if (args.length === 1) {
if (typeof(args[0]) === "number") {
return function(x) {
return x + args[0];
}
} else {
return undefined;
}
}
return false;
}
addTogether(2,3);
Any idea would be very appreciated, thank you in advance.