2

I understand the basic concept of modulo: It give you the remainder with division. I don't seem to be able to grasp how to use it correctly in practice. For instance, the following code takes a number and if it divides evenly by 2 it will return true, otherwise it returns false:

if(number % 2){
return false;
}
else{
return true;
}

It seems to me intuitively (and wrongly) that the way you would code it would be to set it so the modulo works out to 0:

if (number/2 %0) {
   return true

Can anyone explain how and why the first one is correct? Keep in mind that I am obviously extremely dense ...

  • This has been covered before: http://stackoverflow.com/questions/14271865/javascript-modulo – JFlox Jun 06 '15 at 19:46

4 Answers4

2

To check if a number divides without leaving a remainder you need to check if the result of the modulo devision is equal to zero.

if ((number % 2) == 0){
    return true; // number was even
} else {
    return false; // number was odd
}
bhspencer
  • 13,086
  • 5
  • 35
  • 44
  • Cosmetic detail - you do not need to put the modulo operation into braces - it evaluates just like any other arithmetic operation. – Robert Rossmann Jun 06 '15 at 19:53
  • Yeah I know but I like to be explicit with conditions that contain multiple operators. It just makes it obvious to the reader who might not know the order of operations. – bhspencer Jun 06 '15 at 19:54
2

From mdn % remainder documentation:

The remainder operator returns the first operand modulo the second operand, that is, var1 modulo var2, in the preceding statement, where var1 and var2 are variables. The modulo function is the integer remainder of dividing var1 by var2.

Taking that in mind, here are the results of using % with a few values:

59 % 2
> 1
60 % 2
> 0

0 is a falsey value in javascript, so the the result of 60 %2 is never going to pass your if test. To make a proper comparison you'll need to directly check if you have a value of 0:

if (number % 2 === 0)
  return true
Nick Tomlin
  • 28,402
  • 11
  • 61
  • 90
0

When you use the modulo (%) operator, you are basically saying:

number % x:
Divide number by x. Round the result down to nearest integer. Multiply that integer by x. Give me the distance (absolute value) of this number to the original number.

This might not be the exact mathematical definition of modulo, but I would like to believe it is pretty close for our needs.

To give a few examples...

2 % 2 = 0 (2 / 2 = 1, 1 * 2 = 2, abs(2 - 2) = 0)
3 % 2 = 1 (3 / 2 ≐ 1, 1 * 2 = 2, abs(2 - 3) = 1)
4 % 2 = 0 (4 / 2 = 2, 2 * 2 = 4, abs(4 - 4) = 0)

The problem with your notation is that it is one extra operation that needs to be performed by the programmer. Since the current way we express the modulo operation is quite concise and does not require us to do any divisions ourselves, there is potential for performance optimisations to be done under the hood.

To express what I believe is your intent, you basically calculate the modulo with the current syntax and compare it to a particular value:

if (number % 2 === 0)
  return true // Yup, its divisible by 2
Robert Rossmann
  • 11,931
  • 4
  • 42
  • 73
0

Building on top of the other answers, I'd like to mention that using an if / else to explicitly return a boolean value, when you've already evaluated a boolean value, is overly verbose.

If all you are doing in the if / else is returning a boolean on either side, then your return expression can be reduced.

Good:

function isEven(num) {
  return (num % 2 === 0);
}

function isOdd(num) {
  return (num % 2 !== 0);
}

Bad:

function badIsEven(num) {
  if (number % 2 === 0) {
    return true;
  } else {
    return false;
  }
}
Oka
  • 23,367
  • 6
  • 42
  • 53