This code calculates the Grade Weighted Average (GWA) based on the grades and units entered for different subjects. Let's break down the code step by step:
// Rounding Option Retrieval:
const roundingOption = document.getElementById('rounding').value;
// Subject Iteration:
const subjects = document.querySelectorAll('.subject');
// Initializing Variables:
let totalPoints = 0;
let totalUnits = 0;
// Looping Through Subjects:
subjects.forEach(subject => {
// Extracting Grade and Units:
const grade = parseFloat(subject.querySelector('.grade').value) || 0;
const units = parseFloat(subject.querySelector('.units').value) || 0;
// Accumulating Total Points and Units:
totalPoints += grade * units;
totalUnits += units;
});
// Calculating GWA:
let gwa = totalPoints / totalUnits;
// Rounding GWA (Optional):
if (roundingOption === 'rounded') {
gwa = gwa.toFixed(2);
}
// Displaying Result:
const resultElement = document.getElementById('result');
resultElement.textContent = `Your GWA is: ${gwa}`;