Check if a number is even
How to solve it
The most common solution for this Javascript problem is to use the Remainder operator
%
(although slightly different sometimes also referred to as Modulo operator). The Remainder operator returns the remainder of an Integer division. For example:
10 % 3
returns 1 because the number 3 fits no more than 3 times in 10. 3x3 is 9. Thus, the Remainder of 10 % 3
is 10-9=1. Now, how do we make use of the Remainder/Modulo operator to find out if a number is even oder odd? Well, we have to account for the fact that every even number is divisible by 2 while every odd number is not. So,
evenNumber % 2
will always return zero. oddNumber % 2
will always return a number different from zero.