Can someone help me in explaining this code. Thanks in anticipation. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can someone help me in explaining this code. Thanks in anticipation.

var bin="1011"; function bin_to_dec(bin){ var dec=0; var pow=1; var len=bin.length; for(var i=len-1; i==0; i--) { dec+=bin[i]*pow; pow*=2; } return dec; } console.log(bin_to_dec(bin)); //11

30th Apr 2020, 4:25 PM
Fred Temi
Fred Temi - avatar
3 Answers
+ 5
The function bin_to_dec converts an binary number to decimal Through starting at the very right number and multiplying it with 1. Then it takes the next number from the right and mulytplies it with 2. This ks done for all digits and added to dec, so dec is the decimal value of bin. This is returned...
30th Apr 2020, 4:29 PM
Alexander Thiem
Alexander Thiem - avatar
+ 3
You are welcome
30th Apr 2020, 5:06 PM
Alexander Thiem
Alexander Thiem - avatar
+ 1
Thank you!!! That's quite helpful.
30th Apr 2020, 5:04 PM
Fred Temi
Fred Temi - avatar