Skip to content

Commit 6a2cf85

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

2 files changed

Lines changed: 55 additions & 15 deletions

File tree

‎Conversions/BinaryToDecimal.js‎

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
1-
export default function binaryToDecimal(binaryString) {
2-
let decimalNumber = 0
3-
const binaryDigits = binaryString.split('').reverse() // Splits the binary number into reversed single digits
4-
binaryDigits.forEach((binaryDigit, index) => {
5-
decimalNumber += binaryDigit * Math.pow(2, index) // Summation of all the decimal converted digits
6-
})
7-
return decimalNumber
1+
/**
2+
* @function binaryToDecimal
3+
* @description Converts a binary number (base-2) string to its decimal
4+
* (base-10) equivalent without using JavaScript's built-in parseInt.
5+
* @param {string} binary - A string representing a binary number
6+
* @returns {number} - The decimal equivalent of the binary number
7+
* @see https://en.wikipedia.org/wiki/Binary_number
8+
* @example binaryToDecimal("1010") => 10
9+
* @example binaryToDecimal("1111") => 15
10+
* @example binaryToDecimal("0") => 0
11+
*/
12+
const binaryToDecimal = (binary) => {
13+
if (typeof binary !== 'string' || !/^[01]+$/.test(binary)) {
14+
throw new TypeError(
15+
'Input must be a binary string containing only 0s and 1s'
16+
)
17+
}
18+
return binary
19+
.split('')
20+
.reverse()
21+
.reduce((acc, bit, index) => acc + Number(bit) * Math.pow(2, index), 0)
822
}
23+
24+
export { binaryToDecimal }
Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,39 @@
1-
import binaryToDecimal from '../BinaryToDecimal'
1+
import { binaryToDecimal } from '../BinaryToDecimal.js'
22

3-
describe('BinaryToDecimal', () => {
4-
it('expects to return correct decimal value', () => {
5-
expect(binaryToDecimal('1000')).toBe(8)
3+
describe('binaryToDecimal', () => {
4+
test("binaryToDecimal('0') returns 0", () => {
5+
expect(binaryToDecimal('0')).toBe(0)
66
})
77

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

12-
it('expects to return correct hexadecimal value for padding-required binary', () => {
13-
expect(binaryToDecimal('1000101')).toBe(69)
12+
test("binaryToDecimal('1010') returns 10", () => {
13+
expect(binaryToDecimal('1010')).toBe(10)
14+
})
15+
16+
test("binaryToDecimal('1111') returns 15", () => {
17+
expect(binaryToDecimal('1111')).toBe(15)
18+
})
19+
20+
test("binaryToDecimal('100000') returns 32", () => {
21+
expect(binaryToDecimal('100000')).toBe(32)
22+
})
23+
24+
test("binaryToDecimal('11111111') returns 255", () => {
25+
expect(binaryToDecimal('11111111')).toBe(255)
26+
})
27+
28+
test('throws TypeError for non-binary string', () => {
29+
expect(() => binaryToDecimal('1012')).toThrow(TypeError)
30+
})
31+
32+
test('throws TypeError for empty string', () => {
33+
expect(() => binaryToDecimal('')).toThrow(TypeError)
34+
})
35+
36+
test('throws TypeError for non-string input', () => {
37+
expect(() => binaryToDecimal(1010)).toThrow(TypeError)
1438
})
1539
})

0 commit comments

Comments
 (0)