array of 100 positions for 10x10 matrix

1

I have a one-dimensional array of 100 positions and how do I draw a 10x10 array using a for loop.

<!DOCTYPE html>
<html>
<body>

<p id="demo"></p>

<script>
var x= [];

for (i=0; i< 100; i++)
{
x[i] = "X";
}


var texto;

texto = "<table>";

for (i=0; i< (x.length/100); i++)
{

texto += "<tr>";

for (i=(x.length)-1; i< x.length; i++) //como resolver isso? ????
{
texto += "<td>" + x[i] + "</td>";
} 

texto += "</tr>";


}
texto += "</table>";

document.getElementById("demo").innerHTML = texto;

</script>

</body>
</html>
    
asked by anonymous 08.11.2015 / 15:35

2 answers

2

I was able to solve the problem:

<!DOCTYPE html>
<html>
<body>

<p id="demo"></p>

<script>
var x= [];

for (i=0; i< 100; i++)
{
x[i] = "X";
}

x[23] = "Y";
x[88] = "Z";


var texto;

texto = "<table>";

for (i=0; i < (x.length/10); i++)
{
texto += "<tr>";
for (y = (((x.length/100) * (i*10))) ; y < (((x.length/100) * (i+1)) * 10); y++)
{
texto += "<td>" + x[y] + "</td>";
} 
texto += "</tr>";

}
texto += "</table>";

document.getElementById("demo").innerHTML = texto;

</script>

</body>
</html>
    
08.11.2015 / 16:05
1

Basically you will need 2 loops, one to control the vertical direction (table rows) and the other internal to control the horizontal rows (columns):

var x = [];

for (i = 0; i < 100; i++) {
  x[i] = "X" + i;
};


texto = "<table>";

for (i = 0; i < (x.length);) {

  texto += "<tr>";

  for (c = 0; c < 10; c++) //como resolver isso? ????
  {
    texto += "<td>" + x[i] + "</td>";
    i++
  }

  texto += "</tr>";


}
texto += "</table>";
document.getElementById("demo").innerHTML = texto;
<div id="demo"></div>
    
08.11.2015 / 15:56