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: 10 additions & 6 deletions frontend/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,19 +130,23 @@ function updateStatistics(subjects) {
return;
}

const scores = subjects.map(subject => subject.scoreValue);
const highest = Math.max(...scores);

// 計算加權平均
// 計算加權平均與最高分,合併迴圈以提升效能

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.

PR 描述提到已用 pnpm test 驗證,但此 repo 的 package.json 只有 npm testnode --test ...)腳本且看起來未使用 pnpm。請更新 PR 描述的測試指令或補充實際執行的命令,以免後續維護者依描述操作失敗。

Copilot uses AI. Check for mistakes.
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;
}
Comment on lines +136 to +144

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.

highest 初始化為 -Infinity 且只在 score > highest 時更新,會在 scoreValueNaNgetNumericScore 可能回傳 NaN)時永遠不更新,導致 UI 顯示 -Infinity。建議改成在迴圈中用 highest = Math.max(highest, score)(可保留舊的 Math.max(...scores)NaN 的行為且不需要展開陣列),或在更新前用 Number.isFinite(score) 過濾並在最後對 highest === -Infinity 做顯示處理。

Copilot uses AI. Check for mistakes.

const weight = getSubjectWeight(subject.SubjectName);
totalWeightedScore += score * weight;
totalWeight += weight;
});
}
Comment on lines +138 to +149

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

While a standard for loop is performant, using a for...of loop is generally preferred in modern JavaScript for better readability when the index is not required. Given that this codebase already uses ES6+ features (like optional chaining and arrow functions), for...of would be more idiomatic and maintainable without any significant performance penalty for typical array sizes in this context.

    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