This forum is now read-only. Please use our new forums at discuss.codecademy.com.

284 points
52dd588d282ae389a00004c0 121445809
Submitted by
Ratnik Svjetlosti
about 6 years ago

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!
it's about if/else statement


6 votes

permalink

I find answer :))))
if((number % 2) === 0)

284 points
52dd588d282ae389a00004c0 121445809
Submitted by
Ratnik Svjetlosti
about 6 years ago


2 votes

permalink

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);

2435 points
3f20bb02f77b8531b59d907612a24e67?s=140&d=retro
Submitted by
Chase
about 6 years ago

4 Comments

52dd588d282ae389a00004c0 121445809 Ratnik Svjetlosti about 6 years ago

you are not the only one who have a problem with that :/
you have solution here: http://www.codecademy.com/forum_questions/52083167548c3571470059df

3f20bb02f77b8531b59d907612a24e67?s=140&d=retro Chase about 6 years ago

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

52c4b90b9c4e9d37d00059a8 562103961 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.

3f20bb02f77b8531b59d907612a24e67?s=140&d=retro Chase about 6 years ago

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


0 votes

permalink

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';
}
};

358 points
6d9dea3bff76829c1334fea7b2299394?s=140&d=retro
Submitted by
Cajuin
over 4 years ago