Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 22 additions & 22 deletions Conversions/DecimalToOctal.js
Original file line number Diff line number Diff line change
@@ -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
57 changes: 37 additions & 20 deletions Conversions/test/DecimalToOctal.test.js
Original file line number Diff line number Diff line change
@@ -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)
})
})
Loading