How can I do this using the Zoom Out feature?

var ๏ฝ™= '110001'.split("").reverse(); var sum = 0; for (var i = 0; i < y.length; i++) { sum += (y[i] * Math.pow(2, i)); } console.log(sum); 
-2
javascript reduce
source share
2 answers

The easiest way would be to do

 console.log(Array.from('110001').reduce((prev, cur) => prev << 1 | cur)); 

<< is an operator with a left bit, which is substantially multiplied by two here.

Array.from (if available) is preferred over split . This does not matter in this case, but split fails with surrogate pair characters such as ๐Ÿบ, and Array.from will handle them correctly. It can also be written as [...'110001'] , which ends with the same.

Of course you could just say

 parseInt('110001', 2) 
+3
source share

check this snippet

 var binary = '110001'.split("").reverse(); var sum = binary.reduce(function(previous, current, index) { previous = previous + (current * Math.pow(2, index)); return previous; }, 0); console.log(sum); 

Hope this helps

+1
source share

All Articles