Created
June 29, 2018 00:25
-
-
Save pinkmomo027/bc7a6f7b958f4bc82f25d9883306b5e2 to your computer and use it in GitHub Desktop.
print binary
This file contains hidden or 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
let finalResult = []; | |
function printBinary(number) { | |
let chosen = []; | |
printBinaryHelper(number, chosen); | |
} | |
function printBinaryHelper(count, chosen) { | |
if (count == 0) { | |
// base case | |
finalResult.push(chosen.join('')); | |
} else { | |
// choose 0 and explore | |
chosen.push(0); | |
printBinaryHelper(count-1, chosen); | |
// choose 1 and explore | |
chosen.pop(); | |
chosen.push(1); | |
printBinaryHelper(count-1, chosen); | |
chosen.pop(); | |
} | |
} | |
printBinary(4); | |
console.log(finalResult); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment