Skip to content

Instantly share code, notes, and snippets.

@avdg
Created August 15, 2015 22:52
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 avdg/a11df1fff3d8b4641757 to your computer and use it in GitHub Desktop.
Save avdg/a11df1fff3d8b4641757 to your computer and use it in GitHub Desktop.
function duplicate(value, count) {
var output = [];
var duplicator = [value];
var base = 1;
while (count > 0) {
if ((base & count) > 0) {
output = output.concat(duplicator);
count -= base;
}
duplicator = duplicator.concat(duplicator);
base = base << 1; // this was just "base << 1" and didn't store the result, resulting in an infinite loop
}
return output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment