Created
March 12, 2014 14:11
-
-
Save d-simon/9507730 to your computer and use it in GitHub Desktop.
String Repeat
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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