Re: Character Repeat



In comp.lang.javascript message <abc29a16-e0f3-43ac-9db3-9b7573a19db0@s3
7g2000prg.googlegroups.com>, Thu, 31 Jan 2008 15:16:43, RobG
<rgqld@xxxxxxxxxxxx> posted:
On Feb 1, 7:57 am, "bla...@xxxxxxxxxxxx" <bla...@xxxxxxxxxxxx> wrote:
I'm looking for an easy way to repeat a character.

For example in Perl if you want 5 "x"'s the code is
$str = "x" x 5; # result xxxxx

I've tried a few ways in javascript to accomplish this, but can not
come up with anything other then the lame old basic way which is
probably more processing then required...

var str = "";
for (var i = 0; 5 > i; i++){
str += "x";
}

Essentially, that's it.

<Panto> Oh no it isn't ! </Panto>

It can be optimised though: the += compound
operator is notoriously slow in some browsers, an Array with push/join
is usually faster, it shouldn't be slower. A while or do..while loop
should be faster than a for loop, consider something like:

Google : +JavaScript +"Making a Long String" finds
<URL:http://www.merlyn.demon.co.uk/js-misc0.htm#MLS>.

That shows that EvertJan used to know a short, fast method; and I a
faster - that is especially so for big repeats.

function BigCat(L) {
if (!L) return ""
if (L&1) return BigCat(L-1) + "x"
var T = BigCat(L>>1) ; return T + T }

function EjHCat(L) { return new Array(L+1).join('*') }

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk IE6 IE7 FF2 Op9 Sf3
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
.



Relevant Pages

  • Re: How to optimize this JavaScript?
    ... so such a loop is not suited if one deletes ... Because the `for' statement requires three parameters (initialization, ... to JavaScript so what books or websites would you recommend? ...
    (comp.lang.javascript)
  • Re: bind array to select (drop down)?
    ... a select object via a loop. ... If you would want to use javascript anyhow, ... The required `action' attribute is missing from the `form' element. ... ETAGOs in scripts within HTML documents, or XHTML documents served as text/html. ...
    (comp.lang.javascript)
  • Need to extract Querystring
    ... Lacking javascript knowledge, I just realized why my project has a bug. ... I am using ASP to loop through a set of records while it creates URLs ... with a querystring that has a single value pair. ...
    (comp.lang.javascript)
  • Re: getElementsByName()
    ... in the form i have mulitple note elemtents: ... getElementsByTagNameand loop through the result, ... Iterating over all items of the collection and checking their `name' ... Prototype.js was written by people who don't know javascript for people ...
    (comp.lang.javascript)
  • Re: How to optimize this JavaScript?
    ... so such a loop is not suited if one deletes ... to JavaScript so what books or websites would you recommend? ...
    (comp.lang.javascript)