DEV Community

Discussion on: Bin2Dec converter

ekeijl profile image

Reverse the string, so we can start converting bits one by one.
Use Array.reduce to keep track of the sum and each iteration add (current bit * 2index).

const calculateDecimal = binaryString => {
     return [...binaryString].reverse().reduce((sum, bit, i) => sum + (bit * Math.pow(2, i)), 0);
epranka profile image
Edvinas Pranka Author

Nice implementation. Just one thing. The app idea challenge was not store the binary value in the array :) I forget to mention it :)