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

This pull request adds a new evenOrOdd function in the Bit Manipulation folder to check if a number is even or odd using bitwise operations. It includes error handling for wrong inputs and test cases to make sure it works properly. This addition improves our JavaScript algorithms collection! #1729

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

Closed
pvcube66 wants to merge 2 commits into TheAlgorithms:master from pvcube66:master
Closed
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
23 changes: 23 additions & 0 deletions Bit-Manipulation/EvenorOdd.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
*
* This script will check whether a number is even or odd
* using bit manipulation.
* Idea:
* A number can be determined as even or odd by the lsb(least significant bit) or the
* right most bit in binary representation
* if we simply perform an and operation with 1 and the number we can determine:
* eg:
* number & 1 == 1, the number is odd
* number & 1 == 0, the number is even
*
* More about it :
* https://www.geeksforgeeks.org/check-if-a-number-is-odd-or-even-using-bitwise-operators/
*
*/

export const evenOrOdd = (number) => {
if (typeof number !== 'number' || !Number.isInteger(number)) {
throw new Error('Input must be an integer.')
}
return number & 1 ? 'odd' : 'even'
}
39 changes: 39 additions & 0 deletions Bit-Manipulation/test/EvenorOdd.test.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { evenOrOdd } from '../EvenorOdd'

test('check evenOrOdd of 25 is odd', () => {
const res = evenOrOdd(25)
expect(res).toBe('odd')
})

test('check evenOrOdd of 36 is even', () => {
const res = evenOrOdd(36)
expect(res).toBe('even')
})

test('check evenOrOdd of 0 is even', () => {
const res = evenOrOdd(0)
expect(res).toBe('even')
})

test('check evenOrOdd of -13 is odd', () => {
const res = evenOrOdd(-13)
expect(res).toBe('odd')
})

test('check evenOrOdd of 4294967295 is odd', () => {
const res = evenOrOdd(4294967295)
expect(res).toBe('odd')
})

test('check evenOrOdd of -36 is even', () => {
const res = evenOrOdd(-36)
expect(res).toBe('even')
})

test('check evenOrOdd of 21.1 throws error', () => {
expect(() => evenOrOdd(21.1)).toThrow()
})

test('check evenOrOdd of {} throws error', () => {
expect(() => evenOrOdd({})).toThrow()
})

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