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
* @return {boolean}
*/
/*
* Checking if number is even using divisibility by 2
* @function isEven
* @description - Checking if number is even using divisibility by 2
*
* If number is divisible by 2 i.e remainder = 0, then it is even
* 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
* 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
}
/*
* Checking if number is even using bitwise operator
*
/**
* @function isEvenBitwise
* @description - Checking if number is even using bitwise operator
* Bitwise AND (&) compares the bits of the 32
* bit binary representations of the number and
* 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
* odd numbers, which is checked with the use
* of the equality operator.
*
* References:
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_AND
* @param {number} number
* @returns {boolean}
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_AND
*/
export const isEvenBitwise = (number) => {
return (number & 1) === 0
}
export const isEvenBitwise = (number) => (number & 1) === 0

View File

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