title: Create dynamic html table using javascript from simple array
body: <p>I want to write some JavaScript to create a simple HTML table from an array that just contains numbers:</p>
  
  <pre><code>var array = [1,2,3,4,5,6,7,8,9,10];
  </code></pre>
  
  <p>The table should look like this:</p>
  
  <pre><code>1 2 3 4 5
  6 7 8 9 10
  </code></pre>
  
  <p>But the JavaScript code should be dynamic depending on the arrays size (always a factor of 5 though).</p>
  
  <p>I've tried a lot of stuff but it never works the way i want it to. What would be the easiest way to achieve this?</p>
  
  <p>This is one of my tries.</p>
  
  <pre><code>var tableStart = "&lt;table border&gt;";
  for (i = 0; i &lt; arraySize/5; i++){
    var tableMiddle = "&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;3&lt;/td&gt;&lt;td&gt;4&lt;/td&gt;&lt;td&gt;5&lt;/td&gt;&lt;/tr&gt;"
    if (arraySize/5 &gt;= 2) {
      tableMiddle = tableMiddle + tableMiddle;
    }
  };
  var tableEnd = "&lt;/table&gt;";
  var table = tableStart.concat(tableMiddle, tableEnd);
  </code></pre>
  
  <p>as well as</p>
  
  <pre><code>var result = "&lt;table border=1&gt;";
  for(var i=0; i&lt;2; i++) {
      result += "&lt;tr&gt;";
      for(var j=0; j&lt;array.length; j++){
          result += "&lt;td&gt;"+array[i]+"&lt;/td&gt;";
      }
      result += "&lt;/tr&gt;";
  }
  result += "&lt;/table&gt;";
  </code></pre>
  
  <p>this one just results in two values of the array being displayed though a lot of times.</p>