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
30 changes: 23 additions & 7 deletions Conversions/BinaryToDecimal.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
export default function binaryToDecimal(binaryString) {
let decimalNumber = 0
const binaryDigits = binaryString.split('').reverse() // Splits the binary number into reversed single digits
binaryDigits.forEach((binaryDigit, index) => {
decimalNumber += binaryDigit * Math.pow(2, index) // Summation of all the decimal converted digits
})
return decimalNumber
/**
* @function binaryToDecimal
* @description Converts a binary number (base-2) string to its decimal
* (base-10) equivalent without using JavaScript's built-in parseInt.
* @param {string} binary - A string representing a binary number
* @returns {number} - The decimal equivalent of the binary number
* @see https://en.wikipedia.org/wiki/Binary_number
* @example binaryToDecimal("1010") => 10
* @example binaryToDecimal("1111") => 15
* @example binaryToDecimal("0") => 0
*/
const binaryToDecimal = (binary) => {
if (typeof binary !== 'string' || !/^[01]+$/.test(binary)) {
throw new TypeError(
'Input must be a binary string containing only 0s and 1s'
)
}
return binary
.split('')
.reverse()
.reduce((acc, bit, index) => acc + Number(bit) * Math.pow(2, index), 0)
}

export { binaryToDecimal }
40 changes: 32 additions & 8 deletions Conversions/test/BinaryToDecimal.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,39 @@
import binaryToDecimal from '../BinaryToDecimal'
import { binaryToDecimal } from '../BinaryToDecimal.js'

describe('BinaryToDecimal', () => {
it('expects to return correct decimal value', () => {
expect(binaryToDecimal('1000')).toBe(8)
describe('binaryToDecimal', () => {
test("binaryToDecimal('0') returns 0", () => {
expect(binaryToDecimal('0')).toBe(0)
})

it('expects to return correct hexadecimal value for more than one hex digit', () => {
expect(binaryToDecimal('01101000')).toBe(104)
test("binaryToDecimal('1') returns 1", () => {
expect(binaryToDecimal('1')).toBe(1)
})

it('expects to return correct hexadecimal value for padding-required binary', () => {
expect(binaryToDecimal('1000101')).toBe(69)
test("binaryToDecimal('1010') returns 10", () => {
expect(binaryToDecimal('1010')).toBe(10)
})

test("binaryToDecimal('1111') returns 15", () => {
expect(binaryToDecimal('1111')).toBe(15)
})

test("binaryToDecimal('100000') returns 32", () => {
expect(binaryToDecimal('100000')).toBe(32)
})

test("binaryToDecimal('11111111') returns 255", () => {
expect(binaryToDecimal('11111111')).toBe(255)
})

test('throws TypeError for non-binary string', () => {
expect(() => binaryToDecimal('1012')).toThrow(TypeError)
})

test('throws TypeError for empty string', () => {
expect(() => binaryToDecimal('')).toThrow(TypeError)
})

test('throws TypeError for non-string input', () => {
expect(() => binaryToDecimal(1010)).toThrow(TypeError)
})
})
Loading