Skip to content

Instantly share code, notes, and snippets.

@imgsrc
Last active August 23, 2018 19:40
Show Gist options
  • Save imgsrc/c4294a296ce0b8c76ce1567753211880 to your computer and use it in GitHub Desktop.
Save imgsrc/c4294a296ce0b8c76ce1567753211880 to your computer and use it in GitHub Desktop.
decimalToBinary(3); // 11
decimalToBinary(8); // 1000
decimalToBinary(1000); // 1111101000
function decimalToBinary(digit) {
if(digit >= 1) {
if (digit % 2) {
return decimalToBinary((digit - 1) / 2) + 1;
} else {
return decimalToBinary(digit / 2) + 0;
}
} else {
return '';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment