+ 1
Not clear on how this outputs 110, please explain if you can..
function bin (number) { var res = ""; while (num > 0) { res = num % 2 + res; num = Math.floor (num/2); } return res; } document.write (bin(6));
4 Answers
+ 3
you are extracting the binary number of 6 which is 110
i made a code close to this
https://code.sololearn.com/WiQQc1WPBq7r/?ref=app
+ 1
explanation:
6 %2 is 0
6/2=3
3%2=1
you are adding it to the left of the previous value so now res=10
3/2=1
1%2=1
res=110
+ 1
Thank you @Dominique!
But why are we adding to the left of previous value instead of how it's done normally?
+ 1
that is right way to convert into binary. it is also the right way to convert any base number (base 2 for binary, 16 for hexadecimal.)
base 10 is the normal decimal numbers we usually use
im telling you all this in case you wanted to do more research online