If/Else


#1

Instructions: "The if/Else statement should return true if the number it receives is evenly divisible by 2.
Otherwise (else), it should return false."

Here is my code:

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

My error message reads "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."

I discovered that the correct answer comes when I swap the true and false positions.
But it seems to me the instructions state just the opposite, i.e. what I have above.

What am I missing? Thanks!


#2

Remember, you're trying to compare the values in the if statement. Modulo " % " is not a comparative operator, so instead you'll have to do something like this:

if (number % 2 === 0) { ... }

Comparative operators are as follows:
" > " Greater than
" < " Less than
" >= " || " >== " Greater than or equal to
" <= " || " <== " Less than or equal to
" != " || " !== " Not Equal to

Whenever you're creating an "If" statement, it's usually required that you have one of these comparative operators so the computer knows what your talking about, exceptions are infrequent, and unnecessary for the moment.


#3

THANKS! (I remembered most of what the modulo did, but I'd forgotten exactly how to use it.)


#4

this is the right one :wink:
My Code --
var isEven = function(number)
{
if(number % 2 == 0)
{
return true;
}
else
{
return false;
}

};
isEven(2);