Skip to content

Commit e0d23d7

Browse files
feat: add decimal to octal conversion
1 parent 5c39e87 commit e0d23d7

2 files changed

Lines changed: 59 additions & 42 deletions

File tree

‎Conversions/DecimalToOctal.js‎

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
function decimalToOctal(num) {
2-
let oct = 0
3-
let c = 0
1+
/**
2+
* @function decimalToOctal
3+
* @description Converts a decimal number (base-10) to its octal
4+
* (base-8) string representation without using JavaScript's
5+
* built-in toString(8).
6+
* @param {number} decimal - A non-negative integer
7+
* @returns {string} - The octal string representation of the decimal number
8+
* @see https://en.wikipedia.org/wiki/Octal
9+
* @example decimalToOctal(8) => "10"
10+
* @example decimalToOctal(255) => "377"
11+
* @example decimalToOctal(0) => "0"
12+
*/
13+
const decimalToOctal = (decimal) => {
14+
if (!Number.isInteger(decimal) || decimal < 0) {
15+
throw new TypeError('Input must be a non-negative integer')
16+
}
17+
if (decimal === 0) return '0'
18+
let octal = ''
19+
let num = decimal
420
while (num > 0) {
5-
const r = num % 8
6-
oct = oct + r * Math.pow(10, c++)
7-
num = Math.floor(num / 8) // basically /= 8 without remainder if any
21+
octal = (num % 8).toString() + octal
22+
num = Math.floor(num / 8)
823
}
9-
return oct
24+
return octal
1025
}
1126

1227
export { decimalToOctal }
13-
14-
// > decimalToOctal(2)
15-
// 2
16-
17-
// > decimalToOctal(8)
18-
// 10
19-
20-
// > decimalToOctal(65)
21-
// 101
22-
23-
// > decimalToOctal(216)
24-
// 330
25-
26-
// > decimalToOctal(512)
27-
// 1000
Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,43 @@
1-
import { decimalToOctal } from '../DecimalToOctal'
1+
import { decimalToOctal } from '../DecimalToOctal.js'
22

3-
test('The Octal representation of 8 is 10', () => {
4-
const res = decimalToOctal(8)
5-
expect(res).toBe(10)
6-
})
3+
describe('decimalToOctal', () => {
4+
test("decimalToOctal(0) returns '0'", () => {
5+
expect(decimalToOctal(0)).toBe('0')
6+
})
77

8-
test('The Octal representation of 1 is 1', () => {
9-
const res = decimalToOctal(1)
10-
expect(res).toBe(1)
11-
})
8+
test("decimalToOctal(1) returns '1'", () => {
9+
expect(decimalToOctal(1)).toBe('1')
10+
})
1211

13-
test('The Octal representation of 0 is 0', () => {
14-
const res = decimalToOctal(0)
15-
expect(res).toBe(0)
16-
})
12+
test("decimalToOctal(7) returns '7'", () => {
13+
expect(decimalToOctal(7)).toBe('7')
14+
})
1715

18-
test('The Octal representation of 100 is 144', () => {
19-
const res = decimalToOctal(100)
20-
expect(res).toBe(144)
21-
})
16+
test("decimalToOctal(8) returns '10'", () => {
17+
expect(decimalToOctal(8)).toBe('10')
18+
})
19+
20+
test("decimalToOctal(64) returns '100'", () => {
21+
expect(decimalToOctal(64)).toBe('100')
22+
})
23+
24+
test("decimalToOctal(255) returns '377'", () => {
25+
expect(decimalToOctal(255)).toBe('377')
26+
})
27+
28+
test("decimalToOctal(512) returns '1000'", () => {
29+
expect(decimalToOctal(512)).toBe('1000')
30+
})
31+
32+
test('throws TypeError for negative input', () => {
33+
expect(() => decimalToOctal(-1)).toThrow(TypeError)
34+
})
35+
36+
test('throws TypeError for float input', () => {
37+
expect(() => decimalToOctal(1.5)).toThrow(TypeError)
38+
})
2239

23-
test('The Octal representation of 111 is 157', () => {
24-
const res = decimalToOctal(111)
25-
expect(res).toBe(157)
40+
test('throws TypeError for non-number input', () => {
41+
expect(() => decimalToOctal('8')).toThrow(TypeError)
42+
})
2643
})

0 commit comments

Comments
 (0)