Skip to content

Commit f0c29e3

Browse files
authored
chore: Merge PR #629
Added IsEven Function to Maths directory
2 parents df7bf34 + 709de83 commit f0c29e3

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

Maths/IsEven.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Even Number: https://simple.wikipedia.org/wiki/Even_number
3+
*
4+
* function to check if number is even
5+
* return true if number is even
6+
* else false
7+
*/
8+
9+
/**
10+
* @param {number} number
11+
* @return {boolean}
12+
*/
13+
14+
/*
15+
* Checking if number is even using divisibility by 2
16+
*
17+
* If number is divisible by 2 i.e remainder = 0, then it is even
18+
* therefore, the function will return true
19+
*
20+
* If number is not divisible by 2 i.e remainder != 0, then it is not even i.e odd
21+
* therefore, the function will return false
22+
*/
23+
24+
export const isEven = (number) => {
25+
return number % 2 === 0
26+
}
27+
28+
/*
29+
* Checking if number is even using bitwise operator
30+
*
31+
* Bitwise AND (&) compares the bits of the 32
32+
* bit binary representations of the number and
33+
* returns a number after comparing each bit:
34+
*
35+
* 0 & 0 -> 0
36+
* 0 & 1 -> 0
37+
* 1 & 0 -> 0
38+
* 1 & 1 -> 1
39+
*
40+
* For odd numbers, the last binary bit will be 1
41+
* and for even numbers, the last binary bit will
42+
* be 0.
43+
*
44+
* As the number is compared with one, all the
45+
* other bits except the last will become 0. The
46+
* last bit will be 0 for even numbers and 1 for
47+
* odd numbers, which is checked with the use
48+
* of the equality operator.
49+
*
50+
* References:
51+
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_AND
52+
*/
53+
54+
export const isEvenBitwise = (number) => {
55+
return (number & 1) === 0
56+
}

Maths/test/IsEven.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { isEven, isEvenBitwise } from '../IsEven'
2+
3+
test('should return if the number is even or not', () => {
4+
const isEvenNumber = isEven(4)
5+
expect(isEvenNumber).toBe(true)
6+
})
7+
8+
test('should return if the number is even or not', () => {
9+
const isEvenNumber = isEven(7)
10+
expect(isEvenNumber).toBe(false)
11+
})
12+
13+
test('should return if the number is even or not', () => {
14+
const isEvenNumber = isEvenBitwise(6)
15+
expect(isEvenNumber).toBe(true)
16+
})
17+
18+
test('should return if the number is even or not', () => {
19+
const isEvenNumber = isEvenBitwise(3)
20+
expect(isEvenNumber).toBe(false)
21+
})

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