mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 16:26:47 +08:00
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:
11
Conversions/BinaryToDecimal.js
Normal file
11
Conversions/BinaryToDecimal.js
Normal 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')
|
Reference in New Issue
Block a user