Skip to content

Commit 709de83

Browse files
committed
Added jest file in tests, added description and normal even number test(divisibility by 2 )
1 parent 1213574 commit 709de83

File tree

2 files changed

+64
-6
lines changed

2 files changed

+64
-6
lines changed

Maths/IsEven.js

+43-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/* Even Number: https://simple.wikipedia.org/wiki/Even_number
1+
/*
2+
* Even Number: https://simple.wikipedia.org/wiki/Even_number
23
*
34
* function to check if number is even
45
* return true if number is even
@@ -10,10 +11,46 @@
1011
* @return {boolean}
1112
*/
1213

13-
const isEven = (number) => {
14-
return (number & 1) === 0
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
1526
}
1627

17-
// testing
18-
console.log(isEven(4))
19-
console.log(isEven(7))
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

+21
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)