Skip to content

Commit 98c46b4

Browse files
authored
merge: Improved IsOdd function (TheAlgorithms#914)
* refactor: used Boolean function for conversion * feat: added one more function and test cases * test: refactor test case & fixed var names * chore: fixed test placeholder
1 parent cc55dab commit 98c46b4

File tree

2 files changed

+66
-5
lines changed

2 files changed

+66
-5
lines changed

Maths/IsOdd.js

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,45 @@
11
/*
2-
* function to check if number is odd
3-
* return true if number is odd
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.
45
* else false
56
*/
67

7-
export const isOdd = (value) => {
8-
return !!((value & 1))
9-
}
8+
/**
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
13+
*
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}
18+
*/
19+
const isOdd = (number) => Boolean(number % 2) // 1 -> true, 0 -> false
20+
/**
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:
26+
*
27+
* 0 & 0 -> 0
28+
* 0 & 1 -> 0
29+
* 1 & 0 -> 0
30+
* 1 & 1 -> 1
31+
*
32+
* For every odd numbers, the last binary bit will be 1
33+
* and for even numbers, the last binary bit will be 0.
34+
*
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}
42+
*/
43+
const isOddBitwise = (number) => Boolean(number & 1) // 1 -> true, 0 -> false
44+
45+
export { isOdd, isOddBitwise }

Maths/test/IsOdd.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { isOdd, isOddBitwise } from '../IsOdd'
2+
3+
describe('Testing the isOdd function', () => {
4+
it('should return true, if the number is odd', () => {
5+
const isOddNumber = isOdd(4)
6+
expect(isOddNumber).toBe(false)
7+
})
8+
9+
it('should return true, if the number is odd', () => {
10+
const isOddNumber = isOdd(7)
11+
expect(isOddNumber).toBe(true)
12+
})
13+
})
14+
15+
describe('Testing the isOddBitwise function', () => {
16+
it('should return true, if the number is odd', () => {
17+
const isOddNumber = isOddBitwise(6)
18+
expect(isOddNumber).toBe(false)
19+
})
20+
21+
it('should return true, if the number is odd', () => {
22+
const isOddNumber = isOddBitwise(3)
23+
expect(isOddNumber).toBe(true)
24+
})
25+
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy