From e0d23d7098e07d9289ade607a495a88d4b8362a1 Mon Sep 17 00:00:00 2001 From: sarthakgupta1907-cell Date: Mon, 2 Nov 2026 18:30:20 +0530 Subject: [PATCH] feat: add decimal to octal conversion --- Conversions/DecimalToOctal.js | 44 +++++++++---------- Conversions/test/DecimalToOctal.test.js | 57 ++++++++++++++++--------- 2 files changed, 59 insertions(+), 42 deletions(-) diff --git a/Conversions/DecimalToOctal.js b/Conversions/DecimalToOctal.js index 82b3cfe142..348e0d7689 100644 --- a/Conversions/DecimalToOctal.js +++ b/Conversions/DecimalToOctal.js @@ -1,27 +1,27 @@ -function decimalToOctal(num) { - let oct = 0 - let c = 0 +/** + * @function decimalToOctal + * @description Converts a decimal number (base-10) to its octal + * (base-8) string representation without using JavaScript's + * built-in toString(8). + * @param {number} decimal - A non-negative integer + * @returns {string} - The octal string representation of the decimal number + * @see https://en.wikipedia.org/wiki/Octal + * @example decimalToOctal(8) => "10" + * @example decimalToOctal(255) => "377" + * @example decimalToOctal(0) => "0" + */ +const decimalToOctal = (decimal) => { + if (!Number.isInteger(decimal) || decimal < 0) { + throw new TypeError('Input must be a non-negative integer') + } + if (decimal === 0) return '0' + let octal = '' + let num = decimal while (num > 0) { - const r = num % 8 - oct = oct + r * Math.pow(10, c++) - num = Math.floor(num / 8) // basically /= 8 without remainder if any + octal = (num % 8).toString() + octal + num = Math.floor(num / 8) } - return oct + return octal } export { decimalToOctal } - -// > decimalToOctal(2) -// 2 - -// > decimalToOctal(8) -// 10 - -// > decimalToOctal(65) -// 101 - -// > decimalToOctal(216) -// 330 - -// > decimalToOctal(512) -// 1000 diff --git a/Conversions/test/DecimalToOctal.test.js b/Conversions/test/DecimalToOctal.test.js index 0e4214b0c6..dc658ff675 100644 --- a/Conversions/test/DecimalToOctal.test.js +++ b/Conversions/test/DecimalToOctal.test.js @@ -1,26 +1,43 @@ -import { decimalToOctal } from '../DecimalToOctal' +import { decimalToOctal } from '../DecimalToOctal.js' -test('The Octal representation of 8 is 10', () => { - const res = decimalToOctal(8) - expect(res).toBe(10) -}) +describe('decimalToOctal', () => { + test("decimalToOctal(0) returns '0'", () => { + expect(decimalToOctal(0)).toBe('0') + }) -test('The Octal representation of 1 is 1', () => { - const res = decimalToOctal(1) - expect(res).toBe(1) -}) + test("decimalToOctal(1) returns '1'", () => { + expect(decimalToOctal(1)).toBe('1') + }) -test('The Octal representation of 0 is 0', () => { - const res = decimalToOctal(0) - expect(res).toBe(0) -}) + test("decimalToOctal(7) returns '7'", () => { + expect(decimalToOctal(7)).toBe('7') + }) -test('The Octal representation of 100 is 144', () => { - const res = decimalToOctal(100) - expect(res).toBe(144) -}) + test("decimalToOctal(8) returns '10'", () => { + expect(decimalToOctal(8)).toBe('10') + }) + + test("decimalToOctal(64) returns '100'", () => { + expect(decimalToOctal(64)).toBe('100') + }) + + test("decimalToOctal(255) returns '377'", () => { + expect(decimalToOctal(255)).toBe('377') + }) + + test("decimalToOctal(512) returns '1000'", () => { + expect(decimalToOctal(512)).toBe('1000') + }) + + test('throws TypeError for negative input', () => { + expect(() => decimalToOctal(-1)).toThrow(TypeError) + }) + + test('throws TypeError for float input', () => { + expect(() => decimalToOctal(1.5)).toThrow(TypeError) + }) -test('The Octal representation of 111 is 157', () => { - const res = decimalToOctal(111) - expect(res).toBe(157) + test('throws TypeError for non-number input', () => { + expect(() => decimalToOctal('8')).toThrow(TypeError) + }) })