merge: refactor isEven function (#920)

* refactor: formated docs and functions

* refactor: format all test codes
This commit is contained in:
Fahim Faisaal
2022-03-11 22:46:02 +06:00
committed by GitHub
parent 4f6fe1975c
commit da6c22704e
2 changed files with 31 additions and 35 deletions

View File

@ -7,27 +7,22 @@
*/ */
/** /**
* @param {number} number * @function isEven
* @return {boolean} * @description - Checking if number is even using divisibility by 2
*/
/*
* Checking if number is even using divisibility by 2
* *
* If number is divisible by 2 i.e remainder = 0, then it is even * If number is divisible by 2 i.e remainder = 0, then it is even
* therefore, the function will return true * therefore, the function will return true
* *
* If number is not divisible by 2 i.e remainder != 0, then it is not even i.e odd * If number is not divisible by 2 i.e remainder != 0, then it is not even i.e odd
* therefore, the function will return false * therefore, the function will return false
* @param {number} number
* @return {boolean}
*/ */
export const isEven = (number) => number % 2 === 0
export const isEven = (number) => { /**
return number % 2 === 0 * @function isEvenBitwise
} * @description - Checking if number is even using bitwise operator
/*
* Checking if number is even using bitwise operator
*
* Bitwise AND (&) compares the bits of the 32 * Bitwise AND (&) compares the bits of the 32
* bit binary representations of the number and * bit binary representations of the number and
* returns a number after comparing each bit: * returns a number after comparing each bit:
@ -46,11 +41,8 @@ export const isEven = (number) => {
* last bit will be 0 for even numbers and 1 for * last bit will be 0 for even numbers and 1 for
* odd numbers, which is checked with the use * odd numbers, which is checked with the use
* of the equality operator. * of the equality operator.
* * @param {number} number
* References: * @returns {boolean}
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_AND * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_AND
*/ */
export const isEvenBitwise = (number) => (number & 1) === 0
export const isEvenBitwise = (number) => {
return (number & 1) === 0
}

View File

@ -1,21 +1,25 @@
import { isEven, isEvenBitwise } from '../IsEven' import { isEven, isEvenBitwise } from '../IsEven'
test('should return if the number is even or not', () => { describe('Testing isEven function', () => {
const isEvenNumber = isEven(4) it('should return if the number is even or not', () => {
expect(isEvenNumber).toBe(true) const isEvenNumber = isEven(4)
expect(isEvenNumber).toBe(true)
})
it('should return if the number is even or not', () => {
const isEvenNumber = isEven(7)
expect(isEvenNumber).toBe(false)
})
}) })
test('should return if the number is even or not', () => { describe('Testing isEvenBitwise function', () => {
const isEvenNumber = isEven(7) it('should return if the number is even or not', () => {
expect(isEvenNumber).toBe(false) const isEvenNumber = isEvenBitwise(6)
}) expect(isEvenNumber).toBe(true)
})
test('should return if the number is even or not', () => { it('should return if the number is even or not', () => {
const isEvenNumber = isEvenBitwise(6) const isEvenNumber = isEvenBitwise(3)
expect(isEvenNumber).toBe(true) expect(isEvenNumber).toBe(false)
}) })
test('should return if the number is even or not', () => {
const isEvenNumber = isEvenBitwise(3)
expect(isEvenNumber).toBe(false)
}) })