Created
April 4, 2016 03:23
-
-
Save mindplace/740684d0745b8249bd4707d9064a26dd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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