Repeat a string: Difference between revisions

(added Elixir)
Line 596:
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) {
return new Array(1 + (n || 0)).join(this);
}
 
alertconsole.log("ha".repeat(5)); // hahahahaha</lang>
 
As of ES6, `repeat` is built in, so this can be written as:
 
<lang javascript>
console.log("ha".repeat(5)); // hahahahaha</lang>
 
=={{header|Julia}}==
Anonymous user