Created
February 10, 2022 17:20
-
-
Save dev-sampsonorson/fe686526361ed02d9e7f1b4f0186c202 to your computer and use it in GitHub Desktop.
Recursive implementation to convert decimal to binary
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
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