From a572e4f291ef16855cea070fb665732f57e65b04 Mon Sep 17 00:00:00 2001 From: Yatin Date: Mon, 6 Dec 2021 21:36:44 +0530 Subject: [PATCH 1/3] Binary Convert --- Maths/BinaryConvert.js | 31 +++++++++++++++++++++++-------- Maths/test/BInaryConvert.test.js | 2 +- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/Maths/BinaryConvert.js b/Maths/BinaryConvert.js index 4bc80502f4..a5dcea23a4 100644 --- a/Maths/BinaryConvert.js +++ b/Maths/BinaryConvert.js @@ -1,10 +1,25 @@ -const BinaryConvert = (number) => { - const result = [] - let i - for (i = number; i > 0; i = parseInt(i / 2)) { - result.push(i % 2) // push the value (remainder)to array - } return Number(result.reverse().join('')) - // reverse index of array as string ,join and change the type of value to become Number +/** + * @function BinaryConvert + * @description Convert the decimal to binary. + * @param {Integer} num - The input integer + * @return {Integer} - Binary of num. + * @see [Factorial](https://en.wikipedia.org/wiki/Factorial) + * @example BinaryConvert(12) = 1100 + * @example BinaryConvert(12 + 2) = 1110 + */ + +const BinaryConvert = (num) => { + let power = 1 + let binary = 0 + + while (num) { + const rem = num % 2 + num = Math.floor(num / 2) + binary = rem * power + binary + power *= 10 + } + + return binary } -// call function and value as parameter to passing the value + export { BinaryConvert } diff --git a/Maths/test/BInaryConvert.test.js b/Maths/test/BInaryConvert.test.js index 63036754a4..d63cf754ca 100644 --- a/Maths/test/BInaryConvert.test.js +++ b/Maths/test/BInaryConvert.test.js @@ -1,6 +1,6 @@ import { BinaryConvert } from '../BinaryConvert' -describe('Binary Convert', () => { +describe('BinaryConvert', () => { it('should return the correct value', () => { expect(BinaryConvert(12)).toBe(1100) }) From 5a91ffbb3580ea6a8b365d673584047f4bca840e Mon Sep 17 00:00:00 2001 From: Yatin Date: Mon, 6 Dec 2021 21:48:35 +0530 Subject: [PATCH 2/3] Update link --- Maths/BinaryConvert.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Maths/BinaryConvert.js b/Maths/BinaryConvert.js index a5dcea23a4..b1b6ce7fcc 100644 --- a/Maths/BinaryConvert.js +++ b/Maths/BinaryConvert.js @@ -3,7 +3,7 @@ * @description Convert the decimal to binary. * @param {Integer} num - The input integer * @return {Integer} - Binary of num. - * @see [Factorial](https://en.wikipedia.org/wiki/Factorial) + * @see [BinaryConvert](https://www.programiz.com/javascript/examples/decimal-binary) * @example BinaryConvert(12) = 1100 * @example BinaryConvert(12 + 2) = 1110 */ From b3502993ad60e4e23256bbeb60a62f56c983e64d Mon Sep 17 00:00:00 2001 From: Yatin Date: Mon, 13 Dec 2021 23:18:37 +0530 Subject: [PATCH 3/3] add more test cases --- Maths/test/BInaryConvert.test.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Maths/test/BInaryConvert.test.js b/Maths/test/BInaryConvert.test.js index d63cf754ca..53f18e5ffa 100644 --- a/Maths/test/BInaryConvert.test.js +++ b/Maths/test/BInaryConvert.test.js @@ -1,10 +1,22 @@ import { BinaryConvert } from '../BinaryConvert' describe('BinaryConvert', () => { + it('should return the correct value', () => { + expect(BinaryConvert(4)).toBe(100) + }) it('should return the correct value', () => { expect(BinaryConvert(12)).toBe(1100) }) it('should return the correct value of the sum from two number', () => { expect(BinaryConvert(12 + 2)).toBe(1110) }) + it('should return the correct value of the subtract from two number', () => { + expect(BinaryConvert(245 - 56)).toBe(10111101) + }) + it('should return the correct value', () => { + expect(BinaryConvert(254)).toBe(11111110) + }) + it('should return the correct value', () => { + expect(BinaryConvert(63483)).toBe(1111011111111011) + }) })