If / else if / else

<PLEASE USE THIS TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/courses/javascript-beginner-en-qDwp0/0/2?curriculum_id=506324b3a7dffd00020bf661#

<Below this line, in what way does your code behave incorrectly? Include ALL error messages.>
Keeps saying: Oops, try again. Your function returns true for 3! Did I do it all wrong or what is wrong? :o

```

var isEven = function(number) {
if(number %2 === 0) {
return “The number can be dived by 2!”;
}

else if (isNaN(number)) {
return “Please enter a valid number”;
}

else {
return “The number can’t be divided by 2!”;
}
};

isEven(2);

<do not remove the three backticks above>

you should return boolean values (true or false) for odd or even numbers, not strings

2 Likes

Oh… Thanks a lot! My bad :blush:

and strings are considered true, which explains the error message:
Your function returns true (string is true) for 3!

I copied this code, verbatem (even using a code comparer) with only the text changed to boolean values. Still getting this error.

Here is my code

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

isEven(2);

your else clause also returns true, while the else clause will run if the numbers are odd, so it should return false, and if the user doesn’t provide a number, a string should returned (else if clause)

var number=prompt(“Enter your number”);
var isEven = function(number) {
if (isNaN(number)) { return “Enter a corect number!” ;
}
else if (number % 2 === 0 ) {
return “true” ;
}
else {
return “false”;}
};
isEven(number);

This is my code, but return there is an error which return true for 3! I don’t know how to fix this. Can someone help me, pls…??

var isEven = function(number) {
if (number % 2 === 0) {
return true;
}
else if (isNaN(number)) {
return “Input is not a number”
}
else {
return false;
}

};

isEven(test)

actually, the last one “isEven(number)” , should put “” inside the paranthesis. that way, it’ll show “Input is not a number”

Hope that helps

Thank you so much, you saved me, finaly it works. :slight_smile: thx a lot :wink:

Correct Code:
var isEven = function(number) {
// Your code goes here!
if(number % 2 == 0){
return true;
}else if(isNaN(number)){
return “String”;
}else {
return false;
}
};
isEven(3)

it works as butter on bread.

http://www.head-soccer.com/
I had quotes for true and false!

var isEven = function(number) {
// Your code goes here!
if (number % 2 === 0) {
return true;
}

else {
return false;
}
};

isEven(5);

this worked.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.