Skip to content

Commit da6c227

Browse files
authored
merge: refactor isEven function (TheAlgorithms#920)
* refactor: formated docs and functions * refactor: format all test codes
1 parent 4f6fe19 commit da6c227

File tree

2 files changed

+30
-34
lines changed

2 files changed

+30
-34
lines changed

Maths/IsEven.js

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,22 @@
77
*/
88

99
/**
10-
* @param {number} number
11-
* @return {boolean}
12-
*/
13-
14-
/*
15-
* Checking if number is even using divisibility by 2
10+
* @function isEven
11+
* @description - Checking if number is even using divisibility by 2
1612
*
1713
* If number is divisible by 2 i.e remainder = 0, then it is even
1814
* therefore, the function will return true
1915
*
2016
* If number is not divisible by 2 i.e remainder != 0, then it is not even i.e odd
2117
* therefore, the function will return false
18+
* @param {number} number
19+
* @return {boolean}
2220
*/
21+
export const isEven = (number) => number % 2 === 0
2322

24-
export const isEven = (number) => {
25-
return number % 2 === 0
26-
}
27-
28-
/*
29-
* Checking if number is even using bitwise operator
30-
*
23+
/**
24+
* @function isEvenBitwise
25+
* @description - Checking if number is even using bitwise operator
3126
* Bitwise AND (&) compares the bits of the 32
3227
* bit binary representations of the number and
3328
* returns a number after comparing each bit:
@@ -46,11 +41,8 @@ export const isEven = (number) => {
4641
* last bit will be 0 for even numbers and 1 for
4742
* odd numbers, which is checked with the use
4843
* of the equality operator.
49-
*
50-
* References:
51-
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_AND
44+
* @param {number} number
45+
* @returns {boolean}
46+
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_AND
5247
*/
53-
54-
export const isEvenBitwise = (number) => {
55-
return (number & 1) === 0
56-
}
48+
export const isEvenBitwise = (number) => (number & 1) === 0

Maths/test/IsEven.test.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
import { isEven, isEvenBitwise } from '../IsEven'
22

3-
test('should return if the number is even or not', () => {
4-
const isEvenNumber = isEven(4)
5-
expect(isEvenNumber).toBe(true)
6-
})
3+
describe('Testing isEven function', () => {
4+
it('should return if the number is even or not', () => {
5+
const isEvenNumber = isEven(4)
6+
expect(isEvenNumber).toBe(true)
7+
})
78

8-
test('should return if the number is even or not', () => {
9-
const isEvenNumber = isEven(7)
10-
expect(isEvenNumber).toBe(false)
9+
it('should return if the number is even or not', () => {
10+
const isEvenNumber = isEven(7)
11+
expect(isEvenNumber).toBe(false)
12+
})
1113
})
1214

13-
test('should return if the number is even or not', () => {
14-
const isEvenNumber = isEvenBitwise(6)
15-
expect(isEvenNumber).toBe(true)
16-
})
15+
describe('Testing isEvenBitwise function', () => {
16+
it('should return if the number is even or not', () => {
17+
const isEvenNumber = isEvenBitwise(6)
18+
expect(isEvenNumber).toBe(true)
19+
})
1720

18-
test('should return if the number is even or not', () => {
19-
const isEvenNumber = isEvenBitwise(3)
20-
expect(isEvenNumber).toBe(false)
21+
it('should return if the number is even or not', () => {
22+
const isEvenNumber = isEvenBitwise(3)
23+
expect(isEvenNumber).toBe(false)
24+
})
2125
})

0 commit comments

Comments
 (0)