I can see that the return value that I plan to return is a valid value. I print it out just before the return. I think this is closure related since it is happening when a function is being returned and that function is then called. I have given the code below along with the console logging messages for two invocations.
CODE:
function addTogether() {
const [a, b] = arguments;
console.log(`a: ${a} b: ${b}`);
if (typeof a !== 'number') {
return undefined;
}
if (b !== undefined){
if (typeof b !== 'number'){
return undefined;
} else{
let sum = a+b;
console.log("sum being returned =", sum)
return sum;
}
}
return ((b)=>{
addTogether(a,b);
});
}
console.log("returnVal =", addTogether(2,5));
console.log("returnVal =", addTogether(5)(7));
CONSOLE LOG OUTPUT:
a: 2 b: 5
sum being returned = 7
returnVal = 7
a: 5 b: undefined
a: 5 b: 7
sum being returned = 12
returnVal = undefined
**Your browser information:**
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36
Challenge: Arguments Optional
Link to the challenge: