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
36 changes: 22 additions & 14 deletions Conversions/DecimalToBinary.js
Original file line number Diff line number Diff line change
@@ -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'
53 changes: 33 additions & 20 deletions Conversions/test/DecimalToBinary.test.js
Original file line number Diff line number Diff line change
@@ -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)
})
})
Loading