the best easy way to show many same word using javascript
i want to show mant 's' , so my code is :
alert('s'*3)
but it is not running good ,
so i have to :
var str='';
for(var i=0;i<3;i++)
开发者_运维问答 str+='s'
alert(str)
but it is not easy ,
so did you know some more easy way to show many same word ?
thanks
Here is a nice function: http://rosettacode.org/wiki/Repeat_a_string#JavaScript
String.prototype.repeat = function(n) {
return new Array(1 + parseInt(n, 10)).join(this);
}
alert("ha".repeat(5)); // hahahahaha
I usually don't like phpJS much, but they have a very nice 1-line implementation of PHP's str_repeat()
.
If you use the function shown there, you can do a
alert(str_repeat('s', 3));
or if you don't want to copy over the function, steal its contents. This works standalone:
alert(new Array(4).join("s"));
Why not to make a function, that doing your long code variant?
function mult_str(str, count) {
var res = '';
for (var i = 0; i < count; i++) {
res += str;
}
return res;
}
alert(mult_str('s', 3));
精彩评论