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