1

I am trying to generate a table based on arrays and for loops. I have tried my best to solve this on my own With no result. In the for loop you can see that I have tried but With no result. I want the table to only generate from the arrays and for loop and it to look like this:

|Tresort | 1915 | 1950 | 1970 | 1990 | 1992 | 2000 |
|Furu    | 20   | 31   | 53   | 89   | 102  | 117  |
|Gran    | 23   | 39   | 72   | 89   | 92   | 99   |
|Lauvtre | 4    | 6    | 8    | 12   | 15   | 18   |

Here is my code:

<!DOCTYPE html>
<html>
<head>
</head>
<body>

    <table id="tabell" border="1px">


    </table>


    <script>


        function tabellOp() {
            var measurement = [ [20,31,53,89,102,117], [23,39,72,89,92,99], [4,6,8,12,15,18] ];
            var tree = ["furu", "gran", "lauvtre"];
            var year = [1915,1950,1970,1990,1992,2000]
            var tabellget = document.querySelector("#tabell");
            var row="";

            for(var i =0;i<tree.length;i++) {
                row = "<tr><th> Tresort</th></tr>";
                row += "<tr><th>" + tree[i] + "</tr></th>";
            }
            tabellget.innerHTML = row;

        }
        window.onload=tabellOp;


    </script>
</body>
</html>
1
  • 1
    Note that TR is for rows, so you are creating rows but no cells TD Commented Jan 31, 2017 at 0:06

2 Answers 2

0

First of all, your for loop does not make use of the measurement array or the year array - you're just adding the elements from the tree array. The second point - you're making a row for "Tresort" in each loop iteration.

At the end, your result will have the value of

"<tr><th>Tresort</th></tr>
<tr><th>furu</th></tr>
<tr><th>Tresort</th></tr>
<tr><th>gran</th></tr>
<tr><th>Tresort</th></tr>
<tr><th>lauvtre</th></tr>"

which is not what you want. Also, in the second line of your loop you're closing tr tag before closing the th tag, which could also cause errors.

Sign up to request clarification or add additional context in comments.

Comments

0

simple solution your code based (see on jsfiddle)

function tabellOp() {
  var measurement = [ [20,31,53,89,102,117], [23,39,72,89,92,99], [4,6,8,12,15,18] ];
  var tree = ["furu", "gran", "lauvtre"];
  var year = [1915,1950,1970,1990,1992,2000];
  var tabellget = document.querySelector("#tabell");
  var row = "<tr><th> Tresort</th>";

  for (var i = 0; i < year.length; i++) {
    row += "<th>" + year[i] + "</th>";
  }
  row += "</tr>";

  for (var i = 0; i < tree.length; i++) {
    row += "<tr><th>" + tree[i] + "</th>";
    for (var j = 0; j < measurement[i].length; j++) {
      row += "<td>" + measurement[i][j] + "</td>";
    }
    row += "</tr>";
  }

  tabellget.innerHTML = row;
}
window.onload = tabellOp;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.