Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: For loop

  1. #1
    New Coder
    Join Date
    Mar 2006
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    For loop

    I need to create a for loop with a random number from 1-10 that will create hello in a table with cells on whatever number the script generates.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Untitled</title>
    </head>

    <body>
    <script type="text/javascript">
    var n = Math.round(1 + Math.random()* 10);

    for (var i = 1; i <= m; i++)
    {
    txt +=<table>Hello</table>+'<br>';
    }
    document.write(txt)
    </script>

    </body>
    </html>

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    hey... Your table needs at least a row and a cell !!

    <table>
    <tr>
    <td>
    </td>
    </tr>
    </table>

    This is HTML basic....
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    also, txt should be declared and assigned a value prior to concatenating to it, and your saving your random number in 'n' but looping until 'm', that should be fixed.

    Also, you need your html in quotes, and as Kor said you need proper table structure. Here's a slightly improved script. If you need help with the rest, let us know.

    Code:
    <script type="text/javascript">
    var n = Math.round(1 + Math.random()* 10);
    var txt = '';
    for (var i = 1; i <= n; i++)
    {
    txt += '<tr><td>Hello</td></tr>';
    }
    document.write(txt)
    </script>


  •  

    Posting Permissions

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