If the OR is used it does not work. If the “second” variable is check with “typeof” later in the code, it does work.
Why does it not work as an OR but the code does work with each their own IF statement?
More specifically: the test case addTogether(2)([3]) does not work. I would expect it to return undefined, but it doesn’t.
My code so far
function addTogether() {
let [first, second] = arguments;
//here the OR statement does not solve the challenge, but a separate IF statement below does solve the challenge
if (typeof(first) !== "number" || typeof(second) !== "number")
return undefined;
if (arguments.length === 1)
return (second) => addTogether(first, second);
// comment: if I use the code commented out below, it does work as expected and solves the challenge
// if (typeof(second) !== "number")
// return undefined;
return first + second;
}
addTogether(2,3);
If a value was returned, why did 1 function call affect another function call?
I added console.log() in a few places to try to understand why this was happening. Notice it’s returning “undefined” for addTogether(2)([3]) like it should, BUT Free Code Camp is saying this is wrong.
function addTogether() {
let [first, second] = arguments;
//here the OR statement does not solve the challenge, but a separate IF statement below does solve the challenge
if (typeof(first) !== "number" || typeof(second) !== "number"){
console.log("undefined for " + first + " and " + second);
return undefined;
}
if (arguments.length === 1) {
console.log("called again for " + "second");
return (second) => addTogether(first, second);
}
// comment: if I use the code commented out below, it does work as expected and solves the challenge
// if (typeof(second) !== "number")
// return undefined;
console.log("Adds " + first + " and " + second);
return first + second;
}
addTogether(2,3);