mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-06 01:18:23 +08:00
9 lines
377 B
JavaScript
9 lines
377 B
JavaScript
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
|
|
}
|