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>
This commit is contained in:
neha-saggam
2020-08-21 00:12:01 +05:30
committed by GitHub
parent 26b05a557d
commit 157345cfc1

View File

@ -0,0 +1,11 @@
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')