Tell us what’s happening:
Hello,
I would like to add arguments together, if there is two arguments then add a+b, but if there is more than 2 arguments, the result here is undefined I don’t understand why ?
Could anyone please help. Thank you.
Your code so far
function addTogether() {
// Function to check if a number is actually a number
function checkNum(num) {
return (typeof num === "number");
};
if (arguments.length > 1) {
// Check if we have two arguments and if they are numbers
// Return the sum if they are both numbers
let args = [... arguments]
if (checkNum(args)) {
return args.reduce((a,b)=> a+b,0);
} else {
return undefined;
}
} else if (arguments.length === 1) {
// If only one argument was found, return a new function
let first = arguments[0];
if (checkNum(first)) {
// Return function that expect a second argument.
function addSecond(second) {
// Check if the new argument is a number
if (checkNum(second)) {
return first + second;
} else {
return undefined;
}
};
return addSecond;
} else {
return undefined;
}
}
}
console.log(addTogether(2, 3));
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36
.
Challenge: Arguments Optional
Link to the challenge: