If / else if / else please help


#1

Please help I don't know why this isn't working.

var isEven = function(number) {
// Your code goes here!
if (number % 2 === 0){
return "true"
}
else if(isNaN(number));{
return "Um, that is not a number"
}
} else {
return "false"
}
};
isEven(prompt("Type a number"));

Here is the error notice if it helps: SyntaxError: Unexpected token else

I believe it means the "else" on line 9. Thx in advance


#2

The problem is from your else if to your else:

Problem:

else if(isNaN(number));{   <---- Unneeded Semicolon

Also, take out the curly bracket } before your else as you already have one on the line before.

Correct Code:

var isEven = function(number) {
// Your code goes here!
if (number % 2 === 0){
return "true"
}
else if(isNaN(number)){
return "Um, that is not a number"
}
else {
return "false"
}
};
isEven(prompt("Type a number"));

#3

thanks and i literally just quoted you before I saw this :grinning:


#4

Your Welcome! Just Post and I'll Help If I See It.


#5

lol i know u just helped and i know this code works but can you explain to me why the error notice now says

Oops, try again. Your function returns true for 3!?

when it does not return true at all.


#7

Whoops! Nevermind, the problem is that you have quotes around true and false, which make them into strings, and strings always return true. True and false are booleans, which don't need quotes.


#8

ooohh thx :grinning:


#9

IT WORKED Thank you so much


#10

@absolutebossskill anytime! :smile:


#11

Had this same problem, thanks man :smile: now is working


#12

var isEven = function(number) {
if(number % 2){return true;}

else{return false;}

};

isEven(prompt("type a number"))

whats wrong?


#13

var isEven = function(number) {

if (number % 2 === 0) {
return true;
}else if (isNaN(number) === false){
return "That is not a number";
}else {
return false;
}

};

the error prompt i get is, your code returns true for 3.

anyone know the reason?
thank you


#14

Hey hey,

I have a similar problem and I don't know why I get Error msg: TypeError: NaN is not a function

My code is:
var NaN = function(number) {
// Your code goes here!
if (number%2 ===0){
return true;}
else if (isNaN(number)){
return "Not a number"; }
else {
return false;}
};

NaN("Erika");

Thank you!


#15

on line 5, it has to be equal to true to show that if the number is a string.


#17

#18

This topic is really old, if you still have a question go here and make a new topic