Hello,
I recently completed the Arguments Optional problem. However, I am not 100% sure why it works and I was hoping someone could explain it to me.
My solution:
function addTogether(a,b) {
if (typeof a ==='number' && typeof b === 'number') {
return a + b;
}
else if (typeof a ==='number' && typeof b ==='undefined') {
return function(b) {
if (typeof b === 'object') {
return undefined;
} else {
return a + b;
}
};
}
}
addTogether(2, 3);
If you look at this specific part below. I am checking typeof b for ‘undefined’ yet for some reason after I check it and it is undefined, if I check it again in the inner if statement, it is suddenly an object in some cases. Why would it randomly change from undefined to object?
else if (typeof a ==='number' && typeof b ==='undefined') {
return function(b) {
if (typeof b === 'object') {
return undefined;
} else {
return a + b;
}
};