Skip to content

Instantly share code, notes, and snippets.

@rpf5573 rpf5573/10to2.js
Last active Jan 15, 2019

Embed
What would you like to do?
function transform(input) {
var result = [0];
for ( var i = 0; i < input; i++ ) {
up(result);
}
return result.reverse();
}
function up(array) {
array[0]++;
for ( var i = 0; i < array.length; i++ ) {
if ( array[i] == 2 ) {
array[i] = 0;
if ( array[i+1] ) {
array[i+1]++;
}else {
array[i+1] = 1;
}
} else {
return;
}
}
}
transform(8); // [1, 0, 0, 0];
transform(20); // [1, 0, 1, 0, 0];
transform(41); // [1, 0, 1, 0, 0, 1];
transform(65); // [1, 0, 0, 0, 0, 0, 1];
// done in 0.078 seconds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.