mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-04 07:29:47 +08:00

* chore: Switch to Node 20 + Vitest * chore: migrate to vitest mock functions * chore: code style (switch to prettier) * test: re-enable long-running test Seems the switch to Node 20 and Vitest has vastly improved the code's and / or the test's runtime! see #1193 * chore: code style * chore: fix failing tests * Updated Documentation in README.md * Update contribution guidelines to state usage of Prettier * fix: set prettier printWidth back to 80 * chore: apply updated code style automatically * fix: set prettier line endings to lf again * chore: apply updated code style automatically --------- Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com>
46 lines
1.4 KiB
JavaScript
46 lines
1.4 KiB
JavaScript
/*
|
|
* Odd Number: https://simple.wikipedia.org/wiki/Odd_number
|
|
* function to check if number is odd.
|
|
* return true if number is odd.
|
|
* else false
|
|
*/
|
|
|
|
/**
|
|
* @function isOdd
|
|
* @description -> Checking if number is odd using not divisibility by 2
|
|
* If number is not divisible by 2 i.e remainder = 1, then it is odd
|
|
* therefore, the function will return true
|
|
*
|
|
* If number is divisible by 2 i.e remainder != 1, then it is even
|
|
* therefore, the function will return false
|
|
* @param {number} number
|
|
* @returns {boolean}
|
|
*/
|
|
const isOdd = (number) => Boolean(number % 2) // 1 -> true, 0 -> false
|
|
/**
|
|
* @function isOddBitwise
|
|
* @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:
|
|
*
|
|
* 0 & 0 -> 0
|
|
* 0 & 1 -> 0
|
|
* 1 & 0 -> 0
|
|
* 1 & 1 -> 1
|
|
*
|
|
* For every odd numbers, the last binary bit will be 1
|
|
* and for even numbers, the last binary bit will be 0.
|
|
*
|
|
* As the number is compared with one, all the
|
|
* other bits except the last will become 0. The
|
|
* last bit will be 0 for even numbers and 1 for
|
|
* odd numbers.
|
|
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_AND
|
|
* @param {number} number
|
|
* @returns {boolean}
|
|
*/
|
|
const isOddBitwise = (number) => Boolean(number & 1) // 1 -> true, 0 -> false
|
|
|
|
export { isOdd, isOddBitwise }
|