Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions frontend/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Comment on lines +133 to +140

Copilot AI Apr 2, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updateStatistics behavior is being refactored/optimized here, but there’s currently no automated test coverage validating the computed highestScore and weighted average outputs (existing tests for dashboard.js only cover getNumericScore/shortenName). To prevent regressions from this optimization, consider adding a unit test that exercises updateStatistics via a minimal DOM (e.g., JSDOM), or extracting the pure computation into a separately testable helper and keeping the DOM updates thin.

Copilot uses AI. Check for mistakes.

const weight = getSubjectWeight(subjects[i].SubjectName);
totalWeightedScore += score * weight;
totalWeight += weight;
});
}
Comment on lines +138 to +145

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

在迴圈中多次透過索引存取 subjects[i] 會增加重複的尋找開銷。建議改用 for...of 迴圈,這不僅能提升效能(避免索引查找),也能顯著增加程式碼的可讀性,更符合此 PR 優化效能與程式碼品質的初衷。

Suggested change
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;
});
}
for (const subject of subjects) {
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;

Expand Down
Loading