I'm creating an online calculator to get the average grade of different tests my students take. I wonder if you see any problems in the following code or any way to improve it:
document.getElementById('calculator').addEventListener('click', function() {
var physicsTests = document.getElementById('physics').getElementsByTagName('input'),
historyTests = document.getElementById('history').getElementsByTagName('input'),
physicsTestsCount = 0,
historyTestsCount = 0,
physicsAverage = document.getElementById('physicsAverage'),
historyAverage = document.getElementById('historyAverage'),
i;
for (i = 0; i < physicsTests.length; i++) {
if (physicsTests[i].value) {
physicsTestsCount++;
}
if (!physicsTestsCount) {
physicsAverage.value = 'No assessment made!';
} else {
physicsAverage.value = (Number(physicsTests[0].value) + Number(physicsTests[1].value) + Number(physicsTests[2].value)) / physicsTestsCount;
}
}
for (i = 0; i < historyTests.length; i++) {
if (historyTests[i].value) {
historyTestsCount++;
}
if (!historyTestsCount) {
historyAverage.value = 'No assessment made!';
} else {
historyAverage.value = (Number(historyTests[0].value) + Number(historyTests[1].value) + Number(historyTests[2].value)) / historyTestsCount;
}
}
});
<form>
<p id="physics">
Physics:
<input type="number">
<input type="number">
<input type="number">
<output id="physicsAverage"></output>
</p>
<p id="history">
History:
<input type="number">
<input type="number">
<input type="number">
<output id="historyAverage"></output>
</p>
<button type="button" id="calculator">Calculate</button>
</form>