SOULJA SOULJA - 8 days ago 5
HTML Question

Javascript code that should make a table and run on page load

Javascript should run when the page loads, create a table with 10 rows and 10 columns and adds it to div "funky", the cells of the table should contain the text "N M" where N is the minumum of the row and column index and M is the maximum of the row and column index,cells in even numbered columns should have the text colour purple. here are my javascript and html codes



function go() {
var ROWS = 10;
var COLS = 10;
var out = document.getElementsByClassName("funky");
var table = document.createElement("table");
table.border = 1;
for (var i = 0; i <= ROWS; i += 1) {
var row = document.createElement("row");
if (i == 2) {
row.Fontweight = "bold";
}
for (var j = 0; j < COLS; j += 2) {
var col = document.createElement("cell");
col.innerHTML = i + " " + j;
if (j / 2 === 0) {
col.style.color = "purple";
}
row.appendChild(col);
}
table.appendChild(row);
document.getElementsByClassName("table");
}
}
onload = go();

<html>
<head>
<title>Question 2</title>
<script src="question2.js" type="text/javascript"></script>
<head>
<body>
<div class="funky"></div>
</body>
<html>




Answer

Replace HTML Code To :

<html>
  <head>
    <title>Question 2</title>
    <script src="question2.js" type="text/javascript"></script>
  </head>
  <body onload="javascript:go();">
    <div class="funky"></div>
  </body>
</html>

Replace JS Code To :

function go() {
  var ROWS = 10;
  var COLS = 10;
  var out = document.getElementsByClassName("funky")[0];
  var table = document.createElement("table");
  table.style.border = "1px solid #000";
  table.style.borderCollapse = "collapse";
  for (var i = 0; i <= ROWS; i += 1) {
    var row = document.createElement("tr");
    if (i == 2) {
      row.style.fontWeight = "bold";
    }
    for (var j = 0; j < COLS; j += 1) {
      var col = document.createElement("td");
      col.innerHTML = i + " " + j;
      if (j % 2 === 0) {
        col.style.color = "purple";
      }
      row.appendChild(col);
    }
    table.appendChild(row);
  }
  out.appendChild(table);
}

You need to use MOD Operator to understand is it even or not. You need to use style property before using border property.