Skip to content

Instantly share code, notes, and snippets.

@mindplace
Created April 4, 2016 03:23
Show Gist options
  • Save mindplace/740684d0745b8249bd4707d9064a26dd to your computer and use it in GitHub Desktop.
Save mindplace/740684d0745b8249bd4707d9064a26dd to your computer and use it in GitHub Desktop.
function toBinaryNumber(number) {
var returning = new Array;
if (number % 2 != 0) {
returning[0] = 1;
number -= 1;
}
while (number > 0) {
i = 1;
while (Math.pow(2, i) <= number) {
if (Math.pow(2, (i + 1)) > number) {
returning[i] = 1;
number -= (Math.pow(2, i));
break;
}
i++;
}
}
for (var j=0; j < returning.length; j++) {
if (returning[j] == null) {
returning[j] = "0";
} else {
returning[j] = returning[j].toString();
}
}
return parseInt(returning.reverse().join(""));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment