Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

refactor isEven function #920

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
raklaptudirm merged 2 commits into TheAlgorithms:master from fahimfaisaal:upgraded-isEven
Mar 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 12 additions & 20 deletions Maths/IsEven.js
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
32 changes: 18 additions & 14 deletions Maths/test/IsEven.test.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -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)
})

test('should return if the number is even or not', () => {
const isEvenNumber = isEven(7)
expect(isEvenNumber).toBe(false)
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 = isEvenBitwise(6)
expect(isEvenNumber).toBe(true)
})
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(3)
expect(isEvenNumber).toBe(false)
it('should return if the number is even or not', () => {
const isEvenNumber = isEvenBitwise(3)
expect(isEvenNumber).toBe(false)
})
})

AltStyle によって変換されたページ (->オリジナル) /