Your function returns true for 3!


#1


Hi,

I have written the following code in the segment "More on Control Flow in JS" in the second topic of "if/else if/else".
I think I have written the right code because if I put even values, it displays true and if I put odd values (except 3), it displays false but when I put 3 in the function, it displays "The input is not a number" and the browser says "Oops, try again. Your function returns true for 3!"

Some help will be appreciated.

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

isEven(03);

#2

you use isNaN() incorrectly?

this is caused by the fact that strings are considered true, which is a big clue. You return a string (the input is not a number) while it should return false


#4

I think I'm writing the wrong syntax of isNaN function. I understand that strings will be considered true because they're not numbers and numbers will be false because they're not not a number i.e. NaN.

But I don't understand that what should be the right code then? Because the code that I wrote earlier with the wrong isNaN syntax returned the all the right answers except for 3.


#5

isNaN - MDN, see this documentation, it might help you


#6

I got to next segment but still don't fully understand this one.

I put a word (string) in the function and it returns false instead of "The input is not a number."

I converts every string into a number and then checks if it's even or odd.

var isEven = function(number) {

 if (number % 2 === 0) {
     return true;
 } 

 else if (Number.isNaN(number)===true) {
     return "The input is not a number.";
 }

 else {
     return false;
 }

};

isEven("yvfjhbsu");


#7

why Number.? to check if its not a number, simply use isNaN(number) == true


#8

Yup. Got it. Thanks :thumbsup:


#9