If/else in js.what's wrong in my codes


#1

Help please.Whats wrong in my code here

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

Link to exercise: https://www.codecademy.com/courses/javascript-beginner-en-qDwp0/0/1


#2

Hi @carrot017,

When I run your code, I get a message that says "Looks like your function returns false when number = 2", Let's see what happens if we step through your code with number = 2.

number % 2 becomes 2 % 2 which is 0

Because 0 is falsey, number % 2 evaluates to false.

That gives you if (false) and so your function returns what you have in the else block.

Do you see how that works now?


#3

An even number is an integer which is "evenly divisible" by two. This means that if the integer is divided by 2, it yields no remainder. So condition should be compared with 0.


#4

Carrot017: Did you able to fix this issue?


#5

thank you for all of your help ! here is my code:

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


#6

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