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