# 2/14 Your function returns true for 3!

#1

var number;
var isEven = function(number) {
if (number % 2 === 0) {
return true;
}
else if(isNaN(number) == true) {
return "a string";
}
else {
return "number is not even"
};

};

isEven(25);

So it works properly but it says "Oops, try again. Your function returns true for 3!". Why?
Thank you!

#2

@miculdemon,
Try it with

var number;
var isEven = function(number) {
if (number % 2 === 0) {
return true;
}
else if(isNaN(number) == true) {
return "a string";
}
else {
return false;
};

};

console.log( isEven(25) );

As an after-thought,
wouldn't it be better to test isNaN() before doing any arithmetic....??!?

#3

Thank you very much, it works now, but I still don't understand what I was doing wrong. Is it because in the if I made it to return a boolean and in else I returned a string? I am a beginner, can you explain a little more, please? Thank you!

#4

In (1) the instructions
Write an if / else statement inside the isEven function. It should return true; if the number it receives is evenly divisible by 2. Otherwise (else), it should return false;.

Make sure to return - don't use console.log()!

In (2) the instructions
Add an else if branch to your existing if/else statement. If the number put into the function is not a number at all, instead of return true; or return false;, the function should return a string that tells the user that their input isn't a number. (This string can say whatever you like.)

#5

Of course, how silly of me.

Thank you!

#6
var isEven = function(number) {
if (isNaN(number)) {
return "That input is not a number";
} else if (number %2 === 0) {
return true;
} else {
return false;
}
};

isEven("42");

This works as well. Checking whether isNaN is true in the if statement can be simplified further to simply if (isNaN(number)) since the default is true.

#7

Is there a reason or JavaScript rule that the if and the else cannot return strings (like "yep yep" or "nope!"), but rather must return true and false respectively? Or is it simply because it was not what the instructions called for?

I had this lesson working well, and then for fun I replaced my if and else returns with strings and got the same result as the OP...
I don't know how to test anywhere else. Thanks!

#8

Its because of the instructions that expect true for even and false for odd values, the NaN part can be whatever you want it to be (as long as it is a string).

#9

Thank you very much!