# How to use modulo (%) to see if number is divisible by 2 ?

I need to find out is number is even, but I forget how to use modulo !!
Help!

if((number % 2) === 0)

Hey! I think it's like this:

(number % 2 === 0)

Where number is a variable to which you can assign any number.

% is the modulo operator.

=== means 'is equal to'

The 0 means that when var number is divided by 2, there are 0 remainders.

HOPE THAT HELPS!

.......................................................

Can you help me with this? I keep getting an error saying "Oops, try again. Your function returns true for 3!"

But it doesn't! My function returns 'false' for 3 and 'true' for 4.

I think there might be a problem with this lesson.

Here's my code; any help is appreciated!:

var isEven = function(number) {
if (isNaN (number)) {
return "Input was not a number.";

}
else if (number % 2 === 0) {
return "true";

}
else {
return "false";

}
};
isEven(4);

Ratnik Svjetlosti about 6 years ago

you are not the only one who have a problem with that :/

I figured it out! I got it to work by taking the parentheses off of "true" and "false." Thanks!

Tyler Toole about 6 years ago

"These are quotes" (These are parentheses). Just for your reference. That may seem a little nit picky but we are talking programming language here. Nothing is more picky than the computer :p

Oh and thanks for the modulo explanation. It can be easy to forget.

Haha! OOPS! Yes... I meant quotation marks, not parentheses.

I'm also getting Oops, try again. Your function returns true for 3!
this is my code:

var isEven = function(number) {
if( (number % 2) === 0 ) {
return true;
} else if (isNaN(number)) {
return 'Only number aloud!';
}
else {
return 'Number not divisible by 2';
}
};