В основном выученные js за последние 3 часа, поэтому я прошу прощения, если есть какие-либо ошибки, но в основном «-» используется, чтобы указать, что задание не было отправлено, и в этих ячейках мне нужно сделать его желтым, чтобыуказывают на это, я думаю.Вот моя функция javaScript для вычисления среднего значения этих оценок, и если кто-то может помочь, это будет с благодарностью.Также, если кто-то может помочь с эффективностью этого кода и несколько полезных ссылок для изучения JSБольшое спасибо!
getAverageFunction = function()
{
var Table = document.getElementById('gradesTable');
var rowLength =Table.rows.length;
for(var i =1; i<rowLength; i++)
{
var cellsInTable = Table.rows.item(i).cells;
var sum = 0;
for(var j =2; j< cellsInTable.length-1; j++)
{
var valueCell = cellsInTable.item(j).innerHTML;
var currentValue = parseInt(valueCell);
if(currentValue >= 0 && currentValue <=100)
`enter code here`{
sum += currentValue;
}
//trying to turn cells that contain '-' yellow
if(cellsInTable.value === '-')
{
cellsInTable.style.backgroundcolor ="yellow"
}
}
var gradeAverage =0
gradeAverage = sum/5;
if(gradeAverage >= 0 && gradeAverage <40)
{
cellsInTable.item(7).style.backgroundColor = "red";
cellsInTable.item(7).style.color ="white";
console.log(cellsInTable)
}
cellsInTable.item(7).innerHTML = Math.round(gradeAverage) + "%";
}
}
Кроме того, HTML, чтобы пойти с ним
<!DOCTYPE html>
<html>
<head>
<title> Student Assignment Grades </title>
<link rel="stylesheet" href="Table.css">
<script src="getAverageFunction.js"></script>
</head>
<body>
<h1> Student Assignment Grades </h1>
<table id="gradesTable" class="gradesTable" contenteditable>
<caption> <STRONG>Student Grades </STRONG> </caption>
<tr>
<th id="StudentNames" contenteditable="false">Student Names</th>
<th id="StudentID" contenteditable="false"> Student ID</th>
<th id="StudentResults" contenteditable="false"> Assignment 1</th>
<th id="StudentResults" contenteditable="false"> Assignment 2</th>
<th id="StudentResults" contenteditable="false"> Assignment 3</th>
<th id="StudentResults" contenteditable="false"> Assignment 4</th>
<th id="StudentResults" contenteditable="false"> Assignment 5</th>
<th id="Average" contenteditable="false"> Final Grade</th>
</tr>
<tr>
<td contenteditable class="StudentName">"Your Name"</td>
<td contenteditable class="StudentID">"Your Student Number"</td>
<td contenteditable class="Assignment">"-"</td>
<td contenteditable class="Assignment">"-"</td>
<td contenteditable class="Assignment">"-"</td>
<td contenteditable class="Assignment">"-"</td>
<td contenteditable class="Assignment">"-"</td>
<td contenteditable="false" class="Final Grade"> </td>
</tr>
<tr>
<td contenteditable class="StudentName">"Your Name"</td>
<td contenteditable class="StudentID">"Your Student Number"</td>
<td contenteditable class="Assignment">"-"</td>
<td contenteditable class="Assignment">"-"</td>
<td contenteditable class="Assignment">"-"</td>
<td contenteditable class="Assignment">"-"</td>
<td contenteditable class="Assignment">"-"</td>
<td contenteditable="false" class="Final Grade"> </td>
</tr>
<tr>
<td contenteditable class="StudentName">"Your Name"</td>
<td contenteditable class="StudentID">"Your Student Number"</td>
<td contenteditable class="Assignment">"-"</td>
<td contenteditable class="Assignment">"-"</td>
<td contenteditable class="Assignment">"-"</td>
<td contenteditable class="Assignment">"-"</td>
<td contenteditable class="Assignment">"-"</td>
<td contenteditable="false" class="Final Grade"> </td>
</tr>
<tr>
<td contenteditable class="StudentName">"Your Name"</td>
<td contenteditable class="StudentID">"Your Student Number"</td>
<td contenteditable class="Assignment">"-"</td>
<td contenteditable class="Assignment">"-"</td>
<td contenteditable class="Assignment">"-"</td>
<td contenteditable class="Assignment">"-"</td>
<td contenteditable class="Assignment">"-"</td>
<td contenteditable="false" class="Final Grade"> </td>
</tr>
<tr>
<td contenteditable class="StudentName">"Your Name"</td>
<td contenteditable class="StudentID">"Your Student Number"</td>
<td contenteditable class="Assignment">"-"</td>
<td contenteditable class="Assignment">"-"</td>
<td contenteditable class="Assignment">"-"</td>
<td contenteditable class="Assignment">"-"</td>
<td contenteditable class="Assignment">"-"</td>
<td contenteditable="false" class="Final Grade"> </td>
</tr>
<tr>
<td contenteditable class="StudentName">"Your Name"</td>
<td contenteditable class="StudentID">"Your Student Number"</td>
<td contenteditable class="Assignment">"-"</td>
<td contenteditable class="Assignment">"-"</td>
<td contenteditable class="Assignment">"-"</td>
<td contenteditable class="Assignment">"-"</td>
<td contenteditable class="Assignment">"-"</td>
<td contenteditable="false" class="Final Grade"> </td>
</tr>
<tr>
<td contenteditable class="StudentName">"Your Name"</td>
<td contenteditable class="StudentID">"Your Student Number"</td>
<td contenteditable class="Assignment">"-"</td>
<td contenteditable class="Assignment">"-"</td>
<td contenteditable class="Assignment">"-"</td>
<td contenteditable class="Assignment">"-"</td>
<td contenteditable class="Assignment">"-"</td>
<td contenteditable="false" class="Final Grade"> </td>
</tr>
<tr>
<td contenteditable class="StudentName">"Your Name"</td>
<td contenteditable class="StudentID">"Your Student Number"</td>
<td contenteditable class="Assignment">"-"</td>
<td contenteditable class="Assignment">"-"</td>
<td contenteditable class="Assignment">"-"</td>
<td contenteditable class="Assignment">"-"</td>
<td contenteditable class="Assignment">"-"</td>
<td contenteditable="false" class="Final Grade"> </td>
</tr>
<tr>
<td contenteditable class="StudentName">"Your Name"</td>
<td contenteditable class="StudentID">"Your Student Number"</td>
<td contenteditable class="Assignment">"-"</td>
<td contenteditable class="Assignment">"-"</td>
<td contenteditable class="Assignment">"-"</td>
<td contenteditable class="Assignment">"-"</td>
<td contenteditable class="Assignment">"-"</td>
<td contenteditable="false" class="Final Grade"> </td>
</tr>
<tr>
<td contenteditable class="StudentName">"Your Name"</td>
<td contenteditable class="StudentID">"Your Student Number"</td>
<td contenteditable class="Assignment">"-"</td>
<td contenteditable class="Assignment">"-"</td>
<td contenteditable class="Assignment">"-"</td>
<td contenteditable class="Assignment">"-"</td>
<td contenteditable class="Assignment">"-"</td>
<td contenteditable="false" class="Final Grade"> </td>
</tr>
</table>
<!-- button to invoke the calculateAverage Function, styled with a coloured background -->
<button class="btnCenter" onclick="getAverage()"> Calculate Final Grades</button>
</body>
</html>