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

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

// 計算加權平均
// ⚡ Bolt: Combine multiple O(n) array iterations into a single loop
// to avoid intermediate array allocation and reduce iteration overhead.
let highest = -Infinity;
let totalWeightedScore = 0;
let totalWeight = 0;

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;
Comment on lines +135 to 147

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 is initialized to -Infinity and only updated when score > highest. If any subject.scoreValue is NaN (possible if getNumericScore() returns NaN), comparisons will be false and highest may remain -Infinity, which then gets rendered to the UI. Also, totalWeightedScore += score * weight will become NaN and propagate into the average. Consider skipping non-finite scores (e.g., Number.isFinite(score)) and, if no valid scores are found, render '--' (or 0) instead of -Infinity/NaN.

Copilot uses AI. Check for mistakes.
});
}
Comment on lines +139 to +148

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 using a standard for loop is efficient, using for...of is generally preferred in modern JavaScript for better readability when the index is not required. Given that this is a performance-oriented change, for...of is highly optimized in modern engines and would maintain the 'Bolt' performance goals while improving code clarity.

    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