Skip to content

Instantly share code, notes, and snippets.

@rpf5573
Last active January 15, 2019 11:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rpf5573/08fa8fa54ac215e75b36b8ad78eb25ca to your computer and use it in GitHub Desktop.
Save rpf5573/08fa8fa54ac215e75b36b8ad78eb25ca to your computer and use it in GitHub Desktop.
function transform(input) {
if ( input < 2 ) {
return [input]; // 0이나 1은 그냥 돌려 보낸다.
}
var result = [];
for ( var i = 0; 1 <= input; i++ ) {
result[i] = input%2; // 나머지 값을 구해서 바로 배열에 넣는다.
input = Math.floor(input/2); // 소수점은 버리는 방식을 통해, 나머지 값을 제외한 몫만 구한다.
}
return result.reverse(); // 배열을 거꾸로 뒤집어서 돌려준다.
}
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.067 seconds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment