-1

A have an code that build an table with 50 cells, but it have just 1 column/line, and I want to build an table with just 4 lines.

How can I do it with javascript?

function montaTabela() {
document.write("<table border='0' cellspacing='0' cellpadding='0' style=' font-family:Segoe UI, Segoe, Verdana;'>");
for (i=1; i<=50; i++){
    if (i%4 !== 0){
    document.write("<tr><td style='color:#696969;'>Table</td></tr>");
    } else {
        document.write("<tr><td style='color:#ff0000'>Table</td></tr>");

    }
};
documento.writeln("</table>");
}

TKS

1 Answers1

1

Try this. Also, keep in mind that since 50 isn't divisible by 4 you'll have an odd row at the end.

function montaTabela()
{
  document.write("<table border='0' cellspacing='0' cellpadding='0' style='font-family:Segoe UI, Segoe, Verdana;'>");
  document.write("<tr>");
  for (i=0; i<50; i++)
  {
    if (i>0 && i%4 == 0)
    { // if the column index is divisable by four, end the row and start a new one.
      document.write("</tr><tr>");
    }

    document.write("<td style='color:#ff0000'>Table</td>");    
  }
  document.write("</tr>");
  documento.writeln("</table>");
};
JtM
  • 213
  • 1
  • 6