Skip to content

Instantly share code, notes, and snippets.

@d-simon
Created March 12, 2014 14:11
Show Gist options
  • Save d-simon/9507730 to your computer and use it in GitHub Desktop.
Save d-simon/9507730 to your computer and use it in GitHub Desktop.
String Repeat
// http://stackoverflow.com/a/5450113
// String prototyped
String.prototype.repeat = function(count) {
if (count < 1) return '';
var result = '', pattern = this.valueOf();
while (count > 0) {
if (count & 1) result += pattern;
count >>= 1, pattern += pattern;
}
return result;
};
// Standalone
function repeat(pattern, count) {
if (count < 1) return '';
var result = '';
while (count > 0) {
if (count & 1) result += pattern;
count >>= 1, pattern += pattern;
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment