You’re close. We have a logical issue and a syntax issue here:
First the logical issue. The challenges says that
If either argument isn’t a valid number, return undefined.
So the logic for 1. needs to be a bit different.
We might need to make some of the other if {} more complicated, but let’s get there when we get there : )
For the syntax issue, isInteger won’t quite check for numbers, only integers.
You are definitely on the right track! One question to ponder though, what is typeof undefined? If you check for if y is not a number in 1., then will you ever be able to reach 3.?
You have some smart quotes in there, which is causing issues for my code
function addTogether(x, y) {
if (typeof y === 'undefined') {
return function (y) {
return x + y;
}
} else if (typeof x !== 'number' || typeof y === 'number') {
return x + y;
} else {
undefined;
}
}
I adjusted the code so that it’s a bit easier for me to read and I removed the smart quotes.
There are a few little things now
In your first if, you still want to make sure that x is a number.
The function you return should check if ‘y’ is a number.
Is your condition quite what you want for the else if? You need both x and y to be a number in this case, right?
function addTogether(x, y) {
if (typeof x === 'number' && typeof y === 'undefined') {
return function (y) {
if(typeof y === 'number')
return x + y;
}
} else if (typeof x === 'number' || typeof y === 'number') {
return x + y;
} else {
undefined;
}
}
Nope, I meant the logic to say if both are numbers.
function addTogether(x, y) {
if (typeof x === 'number' && typeof y === 'undefined') {
return function (y) {
if(typeof y === 'number')
return x + y;
}
} else if (typeof x === 'number' && typeof y === 'number') {
return x + y;
} else {
undefined;
}
}
Thanks so much for the time you spent helping me out. I really appreciate you helping me understand what I’m actually doing and not just giving me the answer.
just to nitpick, this doesn’t do anything. The only reason for which your function returns undefined correctly it is because that is the default returned value, this snippet above doesn’t return