javascript - Counting in binary -
i'm learning program , trying make program count in binary.
i made function convert provided decimal binary , looks working ok, when try count upwards using loop browser freezes , can't understand why. using similar while loop produces result needed.
the problem right @ bottom commented out. please figure out i'm doing wrong here.
here's code:
function isodd(num) {return num % 2}; var tobinary = function (number) { ints = []; binary = []; ints.push(math.floor(number)); while (number >= 1) { number = (math.floor(number))/2; ints.push(math.floor(number)); } (i=ints.length-1;i>=0;i--) { if (isodd(ints[i])) { binary.push(1); } else { binary.push(0); } } if (binary[0] === 0) { binary.splice(0,1); } return binary; }; var count = 0; while (count <= 50) { console.log(tobinary(count)); count++; } /* (i=1;i<=50;i++) { console.log(tobinary(i)); } */
use for (var i=ints.length-1;i>=0;i--)
, for (var i=1;i<=50;i++)
, otherwise i
global variable , overwritten inside tobinary
.
Comments
Post a Comment