mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-15 02:33:35 +08:00

* Update BinaryToDecimal.js Fix typo in name * Update BinaryToDecimal.js Co-authored-by: vinayak <itssvinayak@gmail.com>
15 lines
496 B
JavaScript
15 lines
496 B
JavaScript
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
|
|
})
|
|
console.log(`Decimal of ${binaryString} is ${decimalNumber}`)
|
|
return decimalNumber
|
|
}
|
|
|
|
(() => {
|
|
binaryToDecimal('111001')
|
|
binaryToDecimal('101')
|
|
})()
|