|
1 | | -/* |
2 | | - * Even Number: https://simple.wikipedia.org/wiki/Even_number |
| 1 | +/** |
| 2 | + * @license GPL-3.0 or later |
3 | 3 | * |
4 | | - * function to check if number is even |
5 | | - * return true if number is even |
6 | | - * else false |
| 4 | + * @description Checking if number is even |
7 | 5 | */ |
8 | 6 |
|
9 | 7 | /** |
10 | | - * @function isEven |
11 | | - * @description - Checking if number is even using divisibility by 2 |
| 8 | + * @function isEven |
| 9 | + * @description Checking if number is even |
| 10 | + * |
| 11 | + * If the division: number / 2 results in remainder being 0 |
| 12 | + * the number is even, otherwise it's odd |
12 | 13 | * |
13 | | - * If number is divisible by 2 i.e remainder = 0, then it is even |
14 | | - * therefore, the function will return true |
| 14 | + * @param {number} number - Value to check |
| 15 | + * @return {boolean} True if number is even else False |
15 | 16 | * |
16 | | - * If number is not divisible by 2 i.e remainder != 0, then it is not even i.e odd |
17 | | - * therefore, the function will return false |
18 | | - * @param {number} number |
19 | | - * @return {boolean} |
| 17 | + * @see https://simple.wikipedia.org/wiki/Even_number to get |
| 18 | + * more details on even numbers |
20 | 19 | */ |
21 | | -export const isEven = (number) => number % 2 === 0 |
| 20 | +const isEven = (number) => { |
| 21 | + if (typeof number !== 'number' || Number.isNaN(number)) |
| 22 | + throw new TypeError('Argument is Not a Number') |
| 23 | + |
| 24 | + return Boolean(number % 2) === false |
| 25 | +} |
22 | 26 |
|
23 | 27 | /** |
24 | | - * @function isEvenBitwise |
25 | | - * @description - Checking if number is even using bitwise operator |
26 | | - * Bitwise AND (&) compares the bits of the 32 |
27 | | - * bit binary representations of the number and |
28 | | - * returns a number after comparing each bit: |
| 28 | + * @function isEvenBitwise |
| 29 | + * @description Checking if number is even using bitwise |
| 30 | + * operation |
29 | 31 | * |
30 | | - * 0 & 0 -> 0 |
31 | | - * 0 & 1 -> 0 |
32 | | - * 1 & 0 -> 0 |
33 | | - * 1 & 1 -> 1 |
| 32 | + * Bitwise AND (&) compares the bits of the 32 bit binary |
| 33 | + * representations of the number and returns a number after |
| 34 | + * comparing each bit: |
| 35 | + * 0 & 0 -> 0 |
| 36 | + * 0 & 1 -> 0 |
| 37 | + * 1 & 0 -> 0 |
| 38 | + * 1 & 1 -> 1 |
| 39 | + * For odd numbers, the LSB (Least Significant Bit) will be 1 |
| 40 | + * and for even numbers, the LSB will be 0. As the number is |
| 41 | + * compared to one, all other bits will become 0 |
| 42 | + * 0 1 1 1 = 7 |
| 43 | + * & & & & |
| 44 | + * 0 0 0 1 = 1 |
| 45 | + * ↓ ↓ ↓ ↓ |
| 46 | + * 0 0 0 1 = odd since LSB is equal to 1 |
34 | 47 | * |
35 | | - * For odd numbers, the last binary bit will be 1 |
36 | | - * and for even numbers, the last binary bit will |
37 | | - * be 0. |
| 48 | + * @param {number} number - Value to check |
| 49 | + * @return {boolean} True if number is even else False |
38 | 50 | * |
39 | | - * As the number is compared with one, all the |
40 | | - * other bits except the last will become 0. The |
41 | | - * last bit will be 0 for even numbers and 1 for |
42 | | - * odd numbers, which is checked with the use |
43 | | - * of the equality operator. |
44 | | - * @param {number} number |
45 | | - * @returns {boolean} |
46 | | - * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_AND |
| 51 | + * @see https://en.wikipedia.org/wiki/Bit_numbering to get |
| 52 | + * details on LSB (Least Significant Bit) and |
| 53 | + * https://en.wikipedia.org/wiki/Bitwise_operation#AND |
| 54 | + * to get details on bitwise AND operator |
47 | 55 | */ |
48 | | -export const isEvenBitwise = (number) => (number & 1) === 0 |
| 56 | +const isEvenBitwise = (number) => { |
| 57 | + if (typeof number !== 'number' || Number.isNaN(number)) |
| 58 | + throw new TypeError('Argument is Not a Number') |
| 59 | + |
| 60 | + return Boolean(number & 1) === false |
| 61 | +} |
| 62 | + |
| 63 | +export { isEven, isEvenBitwise } |
0 commit comments