Skip to content

Commit cdc982c

Browse files
⚡ Bolt: Combine multiple O(n) iterations into a single loop in updateStatistics
Co-authored-by: alvin000009238 <107313913+alvin000009238@users.noreply.github.com>
1 parent 1a2420d commit cdc982c

2 files changed

Lines changed: 12 additions & 8 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+
// 計算加權平均與最高分 (結合多個 O(n) 操作為單一 O(n) 迴圈提升效能)
137134
let totalWeightedScore = 0;
138135
let totalWeight = 0;
136+
let highest = -Infinity;
139137

140-
subjects.forEach(subject => {
138+
for (let i = 0; i < subjects.length; i++) {
139+
const subject = subjects[i];
141140
const score = subject.scoreValue;
141+
142+
if (score > highest) {
143+
highest = score;
144+
}
145+
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

public/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
</noscript>
4141
<script src="https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit" async defer></script>
4242
<script src="/theme-init.js"></script>
43-
<link rel="stylesheet" href="/dist/main-bt8E1vXG.css" id="vite-css">
43+
<link rel="stylesheet" href="/dist/main-D2sAJccc.css" id="vite-css">
4444
<link rel="icon" type="image/x-icon" href="/favicon.ico">
4545
</head>
4646

@@ -528,7 +528,7 @@ <h3 class="section-title">
528528
</footer>
529529
</div>
530530

531-
<script type="module" src="/dist/main-D-w4GNFQ.js"></script>
531+
<script type="module" src="/dist/main-B4TULAwR.js"></script>
532532
</body>
533533

534534
</html>

0 commit comments

Comments
 (0)