From 6c71a452445e9db4729582f66c10ecccafe88866 Mon Sep 17 00:00:00 2001 From: sarthakgupta1907-cell Date: Mon, 2 Nov 2026 18:25:41 +0530 Subject: [PATCH] feat: add decimal to binary conversion --- Conversions/DecimalToBinary.js | 36 +++++++++------- Conversions/test/DecimalToBinary.test.js | 53 +++++++++++++++--------- 2 files changed, 55 insertions(+), 34 deletions(-) diff --git a/Conversions/DecimalToBinary.js b/Conversions/DecimalToBinary.js index 03b0df8bea..c2addf8d75 100644 --- a/Conversions/DecimalToBinary.js +++ b/Conversions/DecimalToBinary.js @@ -1,19 +1,27 @@ -function decimalToBinary(num) { - const bin = [] +/** + * @function decimalToBinary + * @description Converts a decimal number (base-10) to its binary + * (base-2) string representation without using JavaScript's + * built-in toString(2). + * @param {number} decimal - A non-negative integer + * @returns {string} - The binary string representation of the decimal number + * @see https://en.wikipedia.org/wiki/Binary_number + * @example decimalToBinary(10) => "1010" + * @example decimalToBinary(255) => "11111111" + * @example decimalToBinary(0) => "0" + */ +const decimalToBinary = (decimal) => { + if (!Number.isInteger(decimal) || decimal < 0) { + throw new TypeError('Input must be a non-negative integer') + } + if (decimal === 0) return '0' + let binary = '' + let num = decimal while (num > 0) { - bin.unshift(num % 2) - num >>= 1 // basically /= 2 without remainder if any + binary = (num % 2).toString() + binary + num = Math.floor(num / 2) } - return bin.join('') + return binary } export { decimalToBinary } - -// > decimalToBinary(2) -// '10' - -// > decimalToBinary(7) -// '111' - -// > decimalToBinary(35) -// '100011' diff --git a/Conversions/test/DecimalToBinary.test.js b/Conversions/test/DecimalToBinary.test.js index b3a26e1b76..29c05d51ca 100644 --- a/Conversions/test/DecimalToBinary.test.js +++ b/Conversions/test/DecimalToBinary.test.js @@ -1,26 +1,39 @@ -import { decimalToBinary } from '../DecimalToBinary' +import { decimalToBinary } from '../DecimalToBinary.js' -test('The Binary representation of 35 is 100011', () => { - const res = decimalToBinary(35) - expect(res).toBe('100011') -}) +describe('decimalToBinary', () => { + test("decimalToBinary(0) returns '0'", () => { + expect(decimalToBinary(0)).toBe('0') + }) -test('The Binary representation of 1 is 1', () => { - const res = decimalToBinary(1) - expect(res).toBe('1') -}) + test("decimalToBinary(1) returns '1'", () => { + expect(decimalToBinary(1)).toBe('1') + }) -test('The Binary representation of 1000 is 1111101000', () => { - const res = decimalToBinary(1000) - expect(res).toBe('1111101000') -}) + test("decimalToBinary(10) returns '1010'", () => { + expect(decimalToBinary(10)).toBe('1010') + }) -test('The Binary representation of 2 is 10', () => { - const res = decimalToBinary(2) - expect(res).toBe('10') -}) + test("decimalToBinary(15) returns '1111'", () => { + expect(decimalToBinary(15)).toBe('1111') + }) + + test("decimalToBinary(32) returns '100000'", () => { + expect(decimalToBinary(32)).toBe('100000') + }) + + test("decimalToBinary(255) returns '11111111'", () => { + expect(decimalToBinary(255)).toBe('11111111') + }) + + test('throws TypeError for negative input', () => { + expect(() => decimalToBinary(-1)).toThrow(TypeError) + }) + + test('throws TypeError for float input', () => { + expect(() => decimalToBinary(1.5)).toThrow(TypeError) + }) -test('The Binary representation of 17 is 10001', () => { - const res = decimalToBinary(17) - expect(res).toBe('10001') + test('throws TypeError for non-number input', () => { + expect(() => decimalToBinary('10')).toThrow(TypeError) + }) })