mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-18 17:49:40 +08:00
15 lines
443 B
JavaScript
15 lines
443 B
JavaScript
export const 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
|
|
}
|
|
|
|
// > binaryToDecimal('111001')
|
|
// 57
|
|
|
|
// > binaryToDecimal('101')
|
|
// 5
|