JavaScript str_repeat
Returns the input string repeat mult times
1 2 3 4 56 7 8 9 1011 12 13 | function str_repeat ( input, multiplier ) { // Returns the input string repeat mult times // // version: 909.322 // discuss at: http://phpjs.org/functions/str_repeat // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // + improved by: Jonas Raoni Soares Silva (http://www.jsfromhell.com) // * example 1: str_repeat('-=', 10); // * returns 1: '-=-=-=-=-=-=-=-=-=-=' return new Array(multiplier+1).join(input); } |
Examples
Running
1 | str_repeat('-=', 10); |
Should return
1 | '-=-=-=-=-=-=-=-=-=-=' |
Dependencies
No dependencies, you can use this function standalone.
Open syntax issues
php.js uses JsLint to help us keep our code consistent and prevent some common bugs.
Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.
Authors
Thanks to the following developers, you get to have str_repeat goodness in JavaScript.
This way it's faster :)
String.prototype.repeat = function(l){
return new Array(l+1).join(this);
};
alert("jonas".repeat(10));
@ Darell Brogdon: Thanks for pointing that out, I've searched hard, but wasn't able to find similar projects. Now that I did, how would you feel if I included some of your functions to this project? Well creditted of course.
Most functions are already here but I found a couple that would be a great addition.
The reason to continue here is I developed some tools that really help me to publish new functions quickly, and for me it would be a buzz killer to have to maintain this on sourceforge. Exposure here is bigger as well I think.
I do believe however that once php.js reaches version 1.0, housing it on such a platform would be a better idea.


snaky
27 Jul '08