Repeat a string: Difference between revisions

Content added Content deleted
Line 476: Line 476:


=={{header|JavaScript}}==
=={{header|JavaScript}}==
This solution creates an array of n+1 null elements, then joins them using the target string as the delimiter
This solution creates an empty array of length n+1, then uses the array's join method to effectively concatenate the string n times. Note that extending the prototype of built-in objects is not a good idea if the code is to run in a shared workspace.
<lang javascript>String.prototype.repeat = function(n) {
<lang javascript>String.prototype.repeat = function(n) {
return new Array(1 + n).join(this);
return new Array(1 + n).join(this);
Line 482: Line 482:


alert("ha".repeat(5)); // hahahahaha</lang>
alert("ha".repeat(5)); // hahahahaha</lang>



=={{header|K}}==
=={{header|K}}==