Results 1 to 2 of 2

Thread: Append string to the end of string via a loop

  1. #1
    Join Date
    Dec 2008
    Location
    Fremont, CA
    Posts
    30
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default Append string to the end of string via a loop

    Hi all,

    I am trying to append a string with some more string data. This sounds simple, but I am unable to do it.

    This is what i am trying to do:

    Code:
    <script>
    for(var i = 0; i<10;i++){
    appendString += "This is number "+i+"<BR>";
    }
    
    document.write(appendString);
    </script>
    
    I want my output to be,
    This is number 1
    This is number 2
    This is number 3
    .
    .
    .
    However this doesnt work. Can somebody help me understand why ?

  2. #2
    Join Date
    Dec 2008
    Location
    Fremont, CA
    Posts
    30
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default

    OK, I fixed it, looks like the variable needs to be initialized before being using it in the loop.

    fixed code:
    Code:
    <script>
    appendString='';
    for(var i = 0; i<10;i++){
    appendString += "This is number "+i+"<BR>";
    }
    
    document.write(appendString);
    </script>

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •