Skip to content

Instantly share code, notes, and snippets.

@pinkmomo027
Created June 29, 2018 00:25
Show Gist options
  • Save pinkmomo027/bc7a6f7b958f4bc82f25d9883306b5e2 to your computer and use it in GitHub Desktop.
Save pinkmomo027/bc7a6f7b958f4bc82f25d9883306b5e2 to your computer and use it in GitHub Desktop.
print binary
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