Search code examples
javascriptfor-loophtml-table

Creating table using for loop in Javascript


I am trying to create a simple times table in a html document using Javascript. This is my code so far:

<!DOCTYPE>
    <html>
    <head>
        <title>Table</title>
    </head>
    <body>
    <script language="javascript" type="text/javascript">
        for (var a=0; a < 10; a++) {
            document.write("<tr>");
            for(var b=0; b<10; b++) {
                document.write("<td>"a*b"</td>");
        }
        document.write("</tr>");
        }
    </script>
    </body>
    </html>

I looked through the posted questions, but could not find an answer, possibly because I am a beginner programmer and did not understand most of it.


Solution

  • Well, first of all you should insert the tr (rows) and td (cells) into a table element... Something like

    document.write("<table>");
    // your loop here
    document.write("</table>");
    

    There are better ways to do this, though!