Skip to content

Commit 6c71a45

Browse files
feat: add decimal to binary conversion
1 parent 5c39e87 commit 6c71a45

2 files changed

Lines changed: 55 additions & 34 deletions

File tree

‎Conversions/DecimalToBinary.js‎

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
1-
function decimalToBinary(num) {
2-
const bin = []
1+
/**
2+
* @function decimalToBinary
3+
* @description Converts a decimal number (base-10) to its binary
4+
* (base-2) string representation without using JavaScript's
5+
* built-in toString(2).
6+
* @param {number} decimal - A non-negative integer
7+
* @returns {string} - The binary string representation of the decimal number
8+
* @see https://en.wikipedia.org/wiki/Binary_number
9+
* @example decimalToBinary(10) => "1010"
10+
* @example decimalToBinary(255) => "11111111"
11+
* @example decimalToBinary(0) => "0"
12+
*/
13+
const decimalToBinary = (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 binary = ''
19+
let num = decimal
320
while (num > 0) {
4-
bin.unshift(num % 2)
5-
num >>= 1 // basically /= 2 without remainder if any
21+
binary = (num % 2).toString() + binary
22+
num = Math.floor(num / 2)
623
}
7-
return bin.join('')
24+
return binary
825
}
926

1027
export { decimalToBinary }
11-
12-
// > decimalToBinary(2)
13-
// '10'
14-
15-
// > decimalToBinary(7)
16-
// '111'
17-
18-
// > decimalToBinary(35)
19-
// '100011'
Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,39 @@
1-
import { decimalToBinary } from '../DecimalToBinary'
1+
import { decimalToBinary } from '../DecimalToBinary.js'
22

3-
test('The Binary representation of 35 is 100011', () => {
4-
const res = decimalToBinary(35)
5-
expect(res).toBe('100011')
6-
})
3+
describe('decimalToBinary', () => {
4+
test("decimalToBinary(0) returns '0'", () => {
5+
expect(decimalToBinary(0)).toBe('0')
6+
})
77

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

13-
test('The Binary representation of 1000 is 1111101000', () => {
14-
const res = decimalToBinary(1000)
15-
expect(res).toBe('1111101000')
16-
})
12+
test("decimalToBinary(10) returns '1010'", () => {
13+
expect(decimalToBinary(10)).toBe('1010')
14+
})
1715

18-
test('The Binary representation of 2 is 10', () => {
19-
const res = decimalToBinary(2)
20-
expect(res).toBe('10')
21-
})
16+
test("decimalToBinary(15) returns '1111'", () => {
17+
expect(decimalToBinary(15)).toBe('1111')
18+
})
19+
20+
test("decimalToBinary(32) returns '100000'", () => {
21+
expect(decimalToBinary(32)).toBe('100000')
22+
})
23+
24+
test("decimalToBinary(255) returns '11111111'", () => {
25+
expect(decimalToBinary(255)).toBe('11111111')
26+
})
27+
28+
test('throws TypeError for negative input', () => {
29+
expect(() => decimalToBinary(-1)).toThrow(TypeError)
30+
})
31+
32+
test('throws TypeError for float input', () => {
33+
expect(() => decimalToBinary(1.5)).toThrow(TypeError)
34+
})
2235

23-
test('The Binary representation of 17 is 10001', () => {
24-
const res = decimalToBinary(17)
25-
expect(res).toBe('10001')
36+
test('throws TypeError for non-number input', () => {
37+
expect(() => decimalToBinary('10')).toThrow(TypeError)
38+
})
2639
})

0 commit comments

Comments
 (0)