'How to show a table heading but the value show "?"

I have a form to enter scores and when user clicks "Submit" button it shows a table which will contains all entered information. However, my problem is that I want to display the table heading as "average score" but the value of the "average score" column should be displayed "?" .Let me present the following and I want this exercise done in pure JS, please help. I am really grateful and appreciative.

Here is my HTML file:

<body>
    <script src="js/script.js"></script>
    <h1 align="center">Class Marksheet</h1>

    <table align="center">
      <tr>
        <td>Name:</td>
        <td><input name="name" id="name" type="text" /></td>
      </tr>

      <tr>
        <td>Math:</td>
        <td>
          <input name="math" min="0" max="10" id="math" type="number" />
        </td>
      </tr>

      <tr>
        <td>Physics:</td>
        <td>
          <input name="physics" min="0" max="10" id="physics" type="number" />
        </td>
      </tr>

      <tr>
        <td>Chemistry:</td>
        <td>
          <input name="chemical" min="0" max="10" id="chemical" type="number" />
        </td>
      </tr>

      <td>
        <button type="submit" onclick="score_table()">Submit</button>
      </td>
    </table>

 <!--After the user enters the score and submits, this table will be displayed with the 2 buttons below
-->
    <div id="divTable">
      <table id="tableScore" border="5" align="center">
        <th>No</th>
        <th>Name</th>
        <th>Math</th>
        <th>Physics</th>
        <th>Chemistry</th>
        <th>Average Score</th> <!--Only show table heading but the value must be show "?"-->
      </table>

      <!--User press this button and the average score will be calculated and substituted for the value "?" in the "Average Score" column.-->
      <button onclick="showAvg()" align="center">Calculate the average score</button>

      <!--This is to demonstrate the average score >= 8-->
      <button onclick="showBest()" align="center">
        Best student
      </button>
    </div>
  </body>

My JS file:

var testScore = { 
    name: "",
    math: 0,
    physical: 0,
    chemistry: 0,
    avg: 0
};

var i = 1;
// Show the table after "Submit"
function score_table() {
    
    document.getElementById("divTable").style.display="block";

    // Gathering the data
    testScore.name = document.getElementById("name").value;
    testScore.math = document.getElementById("math").value;
    testScore.physical = document.getElementById("physics").value;
    testScore.chemistry = document.getElementById("chemical").value;

    // I know to calcualte the average score but i am struggle to show the value "?"
    testScore.avg = ((parseFloat(testScore.math) + parseFloat(testScore.physical) + parseFloat(testScore.chemistry)) / 3).toFixed(2);
    
    document.getElementById("name").value = "";
    document.getElementById("math").value = "";
    document.getElementById("physics").value = "";
    document.getElementById("chemical").value = "";

    // Add the data after submit
    var table = document.getElementById("tableScore");
    var row = table.insertRow(i);
    var number = row.insertCell(0);
    var name = row.insertCell(1);
    var math = row.insertCell(2);
    var physics = row.insertCell(3);
    var chemistry = row.insertCell(4);
    var avg = row.insertCell(5);
    
// Return the HTML value
    number.innerHTML  = i;
    name.innerHTML  = testScore.name;
    math.innerHTML  = testScore.math;
    physics.innerHTML  = testScore.physical;
    chemistry.innerHTML  = testScore.chemistry;
    avg.innerHTML  = testScore.avg;
    i++;
}

// I want the calculated average score to replace the value "?" in the "Average Score" column
function showAvg() {
  document.getElementById("tableScore").querySelector("th:nth-child(6)").style.display = "block";
    var colAvg = document.getElementById("tableScore").querySelectorAll("td:nth-child(6n)");
    for (var i = 0; i < colAvg.length; i++) {
      colAvg[i].style.display = "block";
    }
}

// If the average score >= 8  it will be red
function showBest() {
    var colAvg = document.getElementById("tableScore").querySelectorAll("td:nth-child(6n)");
    var rowAvg = document.getElementById("tableScore").querySelectorAll("tr:nth-child(1n)");
  
    for (var i = 0; i < colAvg.length; i++) {
      var avg = parseFloat(colAvg[i].innerText);
      if (avg >= 8) {
        rowAvg[i + 1].style.background = "red";
      } else {}
    }
  }

My CSS file:

#divTable {
  display: none;
  text-align: center;
  width: 100%;
}

#tableScore th:nth-child(6),
#tableScore td:nth-child(6) {
  border-collapse: collapse;
  border-spacing: 0;
  width: 100%;
  display: none;
}



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source