k so my code works I just wanted to know why it works;

var isEven = function(number) {

// Your code goes here!

if (number % 2 === 0) {

return true;

} else {

return false;

}

};

k so my code works I just wanted to know why it works;

var isEven = function(number) {

// Your code goes here!

if (number % 2 === 0) {

return true;

} else {

return false;

}

};

The modulo operator (%) divides two numbers and returns the remainder of those values, so if a number returns 0 it means dividend is divisable by the divisor. That means that if a number is moduloed (is that the correct term? Hm...) by 2 and returns 0 it is a multiple of two which makes it even, if it returns 1 it would be odd.

Basically, if the second number is a multiple of the first number it will return 0, if not it will return something else.

Ex.

1 === 101 % 5

=> true

5 === 101 % 5

=> false

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