Files
JavaScript/Conversions/BinaryToDecimal.js
neha-saggam 157345cfc1 Implement binary to decimal (#264)
* Implement binary to decimal

* Add missing end of line

* Add comments for understanding

Co-authored-by: Neha Saggam <neha@technogise.com>
2020-08-21 00:12:01 +05:30

12 lines
449 B
JavaScript

function binaryToDeicmal (binaryNumber) {
let decimalNumber = 0
const binaryDigits = binaryNumber.split('').reverse() // Splits the bnary number in revered single digits
binaryDigits.forEach((binaryDigit, index) => {
decimalNumber += binaryDigit * (Math.pow(2, index)) // Summation of all the decimal converted digits
})
console.log(`Decimal of ${binaryNumber} is ${decimalNumber}`)
}
binaryToDeicmal('111001')
binaryToDeicmal('101')