From 98c46b4d9e9ec9458c8518e758954bb75071e804 Mon Sep 17 00:00:00 2001 From: Fahim Faisaal <57553028+fahimfaisaal@users.noreply.github.com> Date: Fri, 4 Mar 2022 17:04:33 +0600 Subject: [PATCH] merge: Improved `IsOdd` function (#914) * refactor: used Boolean function for conversion * feat: added one more function and test cases * test: refactor test case & fixed var names * chore: fixed test placeholder --- Maths/IsOdd.js | 46 +++++++++++++++++++++++++++++++++++----- Maths/test/IsOdd.test.js | 25 ++++++++++++++++++++++ 2 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 Maths/test/IsOdd.test.js diff --git a/Maths/IsOdd.js b/Maths/IsOdd.js index 5caeb4a6b..faf14e509 100644 --- a/Maths/IsOdd.js +++ b/Maths/IsOdd.js @@ -1,9 +1,45 @@ /* - * function to check if number is odd - * return true if number is odd + * Odd Number: https://simple.wikipedia.org/wiki/Odd_number + * function to check if number is odd. + * return true if number is odd. * else false */ -export const isOdd = (value) => { - return !!((value & 1)) -} +/** + * @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 } diff --git a/Maths/test/IsOdd.test.js b/Maths/test/IsOdd.test.js new file mode 100644 index 000000000..477044b75 --- /dev/null +++ b/Maths/test/IsOdd.test.js @@ -0,0 +1,25 @@ +import { isOdd, isOddBitwise } from '../IsOdd' + +describe('Testing the isOdd function', () => { + it('should return true, if the number is odd', () => { + const isOddNumber = isOdd(4) + expect(isOddNumber).toBe(false) + }) + + it('should return true, if the number is odd', () => { + const isOddNumber = isOdd(7) + expect(isOddNumber).toBe(true) + }) +}) + +describe('Testing the isOddBitwise function', () => { + it('should return true, if the number is odd', () => { + const isOddNumber = isOddBitwise(6) + expect(isOddNumber).toBe(false) + }) + + it('should return true, if the number is odd', () => { + const isOddNumber = isOddBitwise(3) + expect(isOddNumber).toBe(true) + }) +})