DEV Community

Discussion on: Bin2Dec converter

Collapse
 
ekeijl profile image
Edwin

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);
 };
Collapse
 
epranka profile image
Edvinas Pranka

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