Skip to content

Commit dade0f1

Browse files
⚡ Bolt: Combine array iterations in updateStatistics
Co-authored-by: alvin000009238 <107313913+alvin000009238@users.noreply.github.com>
1 parent 74061ea commit dade0f1

1 file changed

Lines changed: 10 additions & 6 deletions

File tree

frontend/dashboard.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,19 +130,23 @@ function updateStatistics(subjects) {
130130
return;
131131
}
132132

133-
const scores = subjects.map(subject => subject.scoreValue);
134-
const highest = Math.max(...scores);
135-
136-
// 計算加權平均
133+
// ⚡ Bolt: Combined array iterations to avoid multiple O(n) passes
134+
// and prevent RangeError from Math.max(...scores) on large arrays
135+
let highest = -Infinity;
137136
let totalWeightedScore = 0;
138137
let totalWeight = 0;
139138

140-
subjects.forEach(subject => {
139+
for (const subject of subjects) {
141140
const score = subject.scoreValue;
141+
142+
// ⚡ Compute max score
143+
if (score > highest) highest = score;
144+
145+
// ⚡ Compute weighted average
142146
const weight = getSubjectWeight(subject.SubjectName);
143147
totalWeightedScore += score * weight;
144148
totalWeight += weight;
145-
});
149+
}
146150

147151
const weightedAvg = totalWeight > 0 ? totalWeightedScore / totalWeight : 0;
148152

0 commit comments

Comments
 (0)