From 8d71d172daf56e21be02ea8d34254fb0ff8437df Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 2 Apr 2026 09:44:22 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Combine=20multiple=20O(n)?= =?UTF-8?q?=20array=20operations=20into=20a=20single=20loop?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaced the `.map()`, `Math.max()`, and `.forEach()` chained array iterations in `updateStatistics` (`frontend/dashboard.js`) with a single standard `for` loop to avoid intermediate array allocations and reduce redundant iteration passes. Co-authored-by: alvin000009238 <107313913+alvin000009238@users.noreply.github.com> --- frontend/dashboard.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/frontend/dashboard.js b/frontend/dashboard.js index 281b9a2..e5babd2 100644 --- a/frontend/dashboard.js +++ b/frontend/dashboard.js @@ -130,19 +130,23 @@ function updateStatistics(subjects) { return; } - const scores = subjects.map(subject => subject.scoreValue); - const highest = Math.max(...scores); - - // 計算加權平均 + // 計算加權平均與最高分,合併迴圈以提升效能 let totalWeightedScore = 0; let totalWeight = 0; + let highest = -Infinity; - subjects.forEach(subject => { + for (let i = 0; i < subjects.length; i++) { + const subject = subjects[i]; const score = subject.scoreValue; + + if (score > highest) { + highest = score; + } + const weight = getSubjectWeight(subject.SubjectName); totalWeightedScore += score * weight; totalWeight += weight; - }); + } const weightedAvg = totalWeight > 0 ? totalWeightedScore / totalWeight : 0;