Skip to content

Commit 5b43e46

Browse files
feat(maths): add type validation and comprehensive tests for isOdd
1 parent 08d8c6b commit 5b43e46

File tree

2 files changed

+83
-39
lines changed

2 files changed

+83
-39
lines changed

Maths/IsOdd.js

Lines changed: 51 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,63 @@
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
65
*/
76

87
/**
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
1316
*
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
1819
*/
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+
2027
/**
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
2631
*
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
3147
*
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
3450
*
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
4255
*/
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+
}
4462

4563
export { isOdd, isOddBitwise }

Maths/test/IsOdd.test.js

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,51 @@
11
import { isOdd, isOddBitwise } from '../IsOdd'
22

3-
describe('Testing the isOdd function', () => {
4-
it('should return true, if the number is odd', () => {
3+
describe('Testing isOdd function', () => {
4+
const randomNumber = Math.floor(Math.random() * 1999999) - 999999
5+
6+
it("will test a value which shouldn't be odd: 4", () => {
57
const isOddNumber = isOdd(4)
68
expect(isOddNumber).toBe(false)
79
})
810

9-
it('should return true, if the number is odd', () => {
11+
it('will test a value which should be odd: 7', () => {
1012
const isOddNumber = isOdd(7)
1113
expect(isOddNumber).toBe(true)
1214
})
15+
16+
it(`will test a random value: ${randomNumber}`, () => {
17+
const isOddNumber = isOdd(randomNumber)
18+
expect(isOddNumber).toBe(Boolean(randomNumber % 2) !== false)
19+
})
20+
21+
it('will test a TypeError for non-number inputs', () => {
22+
expect(() => isOdd('10')).toThrow(TypeError)
23+
expect(() => isOdd(null)).toThrow(TypeError)
24+
expect(() => isOdd(undefined)).toThrow(TypeError)
25+
expect(() => isOdd(NaN)).toThrow(TypeError)
26+
})
1327
})
1428

15-
describe('Testing the isOddBitwise function', () => {
16-
it('should return true, if the number is odd', () => {
29+
describe('Testing isOddBitwise function', () => {
30+
const randomNumber = Math.floor(Math.random() * 1999999) - 999999
31+
32+
it("will test a value which shouldn't be odd: 6", () => {
1733
const isOddNumber = isOddBitwise(6)
1834
expect(isOddNumber).toBe(false)
1935
})
2036

21-
it('should return true, if the number is odd', () => {
37+
it('will test a value which should be odd: 3', () => {
2238
const isOddNumber = isOddBitwise(3)
2339
expect(isOddNumber).toBe(true)
2440
})
41+
42+
it(`will test a random value: ${randomNumber}`, () => {
43+
const isOddNumber = isOddBitwise(randomNumber)
44+
expect(isOddNumber).toBe(Boolean(randomNumber & 1) !== false)
45+
})
46+
47+
it('will test a TypeError for non-number inputs', () => {
48+
expect(() => isOddBitwise('10')).toThrow(TypeError)
49+
expect(() => isOddBitwise({})).toThrow(TypeError)
50+
})
2551
})

0 commit comments

Comments
 (0)