From 2b2a163d1dc016e7bf31aee28dc4329403e97678 Mon Sep 17 00:00:00 2001 From: fahimfaisaal Date: Thu, 3 Mar 2022 22:08:28 +0600 Subject: [PATCH 1/4] refactor: used Boolean function for conversion --- Maths/IsOdd.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Maths/IsOdd.js b/Maths/IsOdd.js index 5caeb4a6ba..227a7f4d64 100644 --- a/Maths/IsOdd.js +++ b/Maths/IsOdd.js @@ -5,5 +5,5 @@ */ export const isOdd = (value) => { - return !!((value & 1)) + return Boolean(value & 1) // 1 -> true, 0 -> false } From db30616883e726a7b8458393ba4f33b1cdcc8e31 Mon Sep 17 00:00:00 2001 From: fahimfaisaal Date: Thu, 3 Mar 2022 22:29:26 +0600 Subject: [PATCH 2/4] feat: added one more function and test cases --- Maths/IsOdd.js | 46 +++++++++++++++++++++++++++++++++++----- Maths/test/IsOdd.test.js | 21 ++++++++++++++++++ 2 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 Maths/test/IsOdd.test.js diff --git a/Maths/IsOdd.js b/Maths/IsOdd.js index 227a7f4d64..faf14e5096 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 Boolean(value & 1) // 1 -> true, 0 -> 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 } diff --git a/Maths/test/IsOdd.test.js b/Maths/test/IsOdd.test.js new file mode 100644 index 0000000000..375f20b5e1 --- /dev/null +++ b/Maths/test/IsOdd.test.js @@ -0,0 +1,21 @@ +import { isOdd, isOddBitwise } from '../IsOdd' + +test('should return if the number is even or not', () => { + const isEvenNumber = isOdd(4) + expect(isEvenNumber).toBe(false) +}) + +test('should return if the number is even or not', () => { + const isEvenNumber = isOdd(7) + expect(isEvenNumber).toBe(true) +}) + +test('should return if the number is even or not', () => { + const isEvenNumber = isOddBitwise(6) + expect(isEvenNumber).toBe(false) +}) + +test('should return if the number is even or not', () => { + const isEvenNumber = isOddBitwise(3) + expect(isEvenNumber).toBe(true) +}) From 2e70119f2f132ec9764fafc4723fcb983136eae4 Mon Sep 17 00:00:00 2001 From: fahimfaisaal Date: Thu, 3 Mar 2022 23:15:10 +0600 Subject: [PATCH 3/4] test: refactor test case & fixed var names --- Maths/test/IsOdd.test.js | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/Maths/test/IsOdd.test.js b/Maths/test/IsOdd.test.js index 375f20b5e1..bbb2acf54f 100644 --- a/Maths/test/IsOdd.test.js +++ b/Maths/test/IsOdd.test.js @@ -1,21 +1,25 @@ import { isOdd, isOddBitwise } from '../IsOdd' -test('should return if the number is even or not', () => { - const isEvenNumber = isOdd(4) - expect(isEvenNumber).toBe(false) -}) +describe('Testing the isOdd function', () => { + it('should return true, if the number is odd or not', () => { + const isOddNumber = isOdd(4) + expect(isOddNumber).toBe(false) + }) -test('should return if the number is even or not', () => { - const isEvenNumber = isOdd(7) - expect(isEvenNumber).toBe(true) + it('should return true, if the number is odd or not', () => { + const isOddNumber = isOdd(7) + expect(isOddNumber).toBe(true) + }) }) -test('should return if the number is even or not', () => { - const isEvenNumber = isOddBitwise(6) - expect(isEvenNumber).toBe(false) -}) +describe('Testing the isOddBitwise function', () => { + it('should return true, if the number is odd or not', () => { + const isOddNumber = isOddBitwise(6) + expect(isOddNumber).toBe(false) + }) -test('should return if the number is even or not', () => { - const isEvenNumber = isOddBitwise(3) - expect(isEvenNumber).toBe(true) + it('should return true, if the number is odd or not', () => { + const isOddNumber = isOddBitwise(3) + expect(isOddNumber).toBe(true) + }) }) From 7f65eb611645ee5ef56a80cbb4160f99d055f6bf Mon Sep 17 00:00:00 2001 From: fahimfaisaal Date: Thu, 3 Mar 2022 23:16:49 +0600 Subject: [PATCH 4/4] chore: fixed test placeholder --- Maths/test/IsOdd.test.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Maths/test/IsOdd.test.js b/Maths/test/IsOdd.test.js index bbb2acf54f..477044b75a 100644 --- a/Maths/test/IsOdd.test.js +++ b/Maths/test/IsOdd.test.js @@ -1,24 +1,24 @@ import { isOdd, isOddBitwise } from '../IsOdd' describe('Testing the isOdd function', () => { - it('should return true, if the number is odd or not', () => { + 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 or not', () => { + 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 or not', () => { + 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 or not', () => { + it('should return true, if the number is odd', () => { const isOddNumber = isOddBitwise(3) expect(isOddNumber).toBe(true) })