diff --git a/frontend/dashboard.js b/frontend/dashboard.js index 281b9a2..2beb39f 100644 --- a/frontend/dashboard.js +++ b/frontend/dashboard.js @@ -130,19 +130,19 @@ function updateStatistics(subjects) { return; } - const scores = subjects.map(subject => subject.scoreValue); - const highest = Math.max(...scores); - - // 計算加權平均 + // 計算最高分與加權平均,合併迴圈以提升效能 + let highest = -Infinity; let totalWeightedScore = 0; let totalWeight = 0; - subjects.forEach(subject => { - const score = subject.scoreValue; - const weight = getSubjectWeight(subject.SubjectName); + for (let i = 0; i < subjects.length; i++) { + const score = subjects[i].scoreValue; + if (score > highest) highest = score; + + const weight = getSubjectWeight(subjects[i].SubjectName); totalWeightedScore += score * weight; totalWeight += weight; - }); + } const weightedAvg = totalWeight > 0 ? totalWeightedScore / totalWeight : 0;