danteMingione danteMingione - 5 months ago 9
Javascript Question

JavaScript If Else with Return

Still doing the Codecademy training for JavaScript and I've hit a road block.

Here's the code:

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

isEven(2);


So I'm referencing the variable "isEven." Then, I'm telling it to check the number and cross-check it with the modulo to check the remainder against 2 to find out if it's even. If it is, for example, 2 like in the example it should return a remainder of zero, therefore the if is true and it returns true. But it returns false every time. There's no warning messages in the code but when I hit save and it checks it it gives me this message:

"Oops, try again. Looks like your function returns false when number = 2. Check whether your code inside the if/else statement correctly returns true if the number it receives is even."

Answer

I think you had the wrong variable name:

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

isEven(2);
Comments