What is the most efficient way to create an N-times-a-character string in JavaScript? -
this question has answer here:
- repeat string - javascript 29 answers
i have char ('-
') , number (80
) of times should repeated in string. need string of char repeated given number of times:
-------------------------------------------------------------------------------------------------------------
i know can result += mychar
x n times in loop. don't think efficient. there better way that?
by efficient mean: fastest
well, best come with, after couple minutes playing, is:
string.prototype.repeat = function (len) { return (new array(len + 1)).join(this); } console.log('-'.repeat(5));
which is, unfortunately, cheapened little @nnnnnn commented correct answer 2 minutes beforehand. sigh...
Comments
Post a Comment