-4
\$\begingroup\$

I'm practicing this question where I'm supposed to make a table programatically and although I'm not getting any errors the table is not showing.

var ROWS = 13;
var COLS = 2;
 

window.onload = function go() {
     var out = document.getElementsByClassName("funky")[0];
     var table = document.createElement("table");
     table.border = 1;
     for (var i=0; i < ROWS; i += 1) {
            var row = document.createElement("tr");
            for (var j=0; j < COLS; j += 1) {
                   var col = document.createElement("td");
                   col.innerHTML = String.fromCharCode(i + j);
                   if (i % 3 == 0) {
                      col.style.color = "red";
                   } else if (i % 3 == 1) {
                      col.style.color = "green";
                   } else if ((j) % 3 == 2) {
                      col.style.color = "blue";
                   }
            } 
            table.appendChild(row);
     }
     table.appendChild(col);
}

\$\endgroup\$

closed as off-topic by Ethan Bierlein, BCdotWEB, mdfst13, forsvarir, Graipher Dec 1 '16 at 16:06

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. After the question has been edited to contain working code, we will consider reopening it." – Ethan Bierlein, BCdotWEB, mdfst13, forsvarir, Graipher
If this question can be reworded to fit the rules in the help center, please edit the question.

1
\$\begingroup\$

It should be something like this.

<script>
var ROWS = 13;
var COLS = 2;


window.onload = function go() {
     var out = document.getElementsByClassName("funky")[0];
     var table = document.createElement("table");
     table.border = 1;
     for (var i=0; i < ROWS; i += 1) {
            var row = document.createElement("tr");
            for (var j=0; j < COLS; j += 1) {
                   var col = document.createElement("td");
                   col.innerHTML = i;
                   if (i % 3 == 0) {
                      col.style.color = "red";
                   } else if (i % 3 == 1) {
                      col.style.color = "green";
                   } else if ((j) % 3 == 2) {
                      col.style.color = "blue";
                   }
                   row.appendChild(col);
            } 
            table.appendChild(row);
     }
     //table.appendChild(col);
     document.body.appendChild(table);
}
</script>
\$\endgroup\$
  • \$\begingroup\$ So my only mistake was "document.body.appendChild(table);" ? \$\endgroup\$ – Inconnu Dec 1 '16 at 14:23
  • 2
    \$\begingroup\$ Please don't answer off-topic questions; this makes it hard for later clean-up. \$\endgroup\$ – BCdotWEB Dec 1 '16 at 14:25
  • 1
    \$\begingroup\$ Okay sorry, I'll delete this then? \$\endgroup\$ – Inconnu Dec 1 '16 at 14:25
  • \$\begingroup\$ yes you may delete, it's more like a question in stack overflow. you have to add col to row. \$\endgroup\$ – Barani Dec 1 '16 at 14:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.