If/else if / else Function returns true for 3


#1

var isEven = function(number) {
// Your code goes here!

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

else if(isNaN(number) === false)
{return "Hi"}
else {return false;}

};

Point out the mistake please


#2

Try your function yourself and see what happens. If it tells you the result is wrong for number=3, then call your function with that argument and compare the result to what the instructions describe.


#3

same mistake here. it tells me it returns true for 3! help please :frowning:

this is my code:

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


#4

Buddy change your
isNaN(number) === true)

As it is the full code is here.

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


#5

i have the ame problem my code is;

var isEven = function(number) {
if(isNaN(number)){
return "The Input Given By You Is Not A Number";
}
else if(number%2===0)
{
return "Input Is Number & Divisible By 2";
}
else
{
return "Sorry";
}
};
var num=isEven(42);
console.log(num);


#6

var isEven = function(number) {
if(number%2===0){
return true;
}
else if(isNaN(number)===true)
{
return "Hi";
}
else
{
return false;
}
};
var num=isEven(42);
console.log(num);

this code worked for me try this


#7

thankyou .! for the replies .! :slightly_smiling:


#8

I just tried exactly what you put and it still rejects it. I have no idea why as it seems to make sense.


#9

If you have

else if ( isNaN(number) == true) or some other variation (=== true, !==false, etc.)

then the error it will give is returns true for 3. However that actual error is "Does not return false for 3".

Step by step we get (we're testing 2-1000 evens, then 3-999 odds, then NaN):

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

Is the number even? For 2-1000 in increments of 2, this returns true as expected but when it tests 3 we get false and move on to the else if...

else if(isNaN(number)===false){
return "Oops! Not a number!";}

The key thing to notice here is the false in the argument of if. When 3 is tested, it returns false when operated on by the isNaN() function since 3 is a number. This is why the error is being thrown. The function returns whatever text you tell it to instead of false as it expects! When you change the argument of if to if ( isNaN(number) == true), isEven(3) will give false on the even check and false on the NaN check leading us to

else {
return false;}

which generates the expected result isEven(3) --> false.

Hope that clears things up!


#10

I couldn't know what's wrong. Sometimes there might be any small spell mistakes or there could be some missing symbols, Check your code line by line. And than try it. Once it rejects again try refreshing your browser and continue


#12

var isEven = function(number) {
// Your code goes here
var even = number%2

if(even === 0){
return true;
}else if(isNaN(number) == true){
return "Not a number";
}else{
return false;
};

};

isEven(8);


#13

Thanks for the explanation. It really helps to learn how a computer thinks about the statements, because it is easy for us to miss one simple step.