Created
December 28, 2015 04:23
-
-
Save leodutra/c54de755d9d64c914616 to your computer and use it in GitHub Desktop.
String repeat polyfill for JavaScript
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
function repeat(str, times) { | |
var n = times | 0; | |
var res = ''; | |
var buffer = str; | |
for (;;) { | |
if (n & 1) res += buffer; | |
if (n >>= 1) buffer += buffer; | |
else break; | |
} | |
return res; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment