Skip to content

Instantly share code, notes, and snippets.

@dev-sampsonorson
Created February 10, 2022 17:20
Show Gist options
  • Save dev-sampsonorson/fe686526361ed02d9e7f1b4f0186c202 to your computer and use it in GitHub Desktop.
Save dev-sampsonorson/fe686526361ed02d9e7f1b4f0186c202 to your computer and use it in GitHub Desktop.
Recursive implementation to convert decimal to binary
const input1 = 233;
const input2 = 0;
const decimalToBinary = (quotient, remainder) => {
if (quotient === 0) {
return (remainder ?? '').toString();
}
return `${decimalToBinary(Math.floor(quotient / 2), quotient % 2)} ${(remainder ?? '').toString()}`;
}
console.log(decimalToBinary(input1));
console.log(decimalToBinary(input2));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment