-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.