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

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

// 計算加權平均
// Combine max calculation and weighted average into a single pass
// to avoid multiple O(n) iterations and Math.max spread limitations.
let highest = -Infinity;
let totalWeightedScore = 0;
let totalWeight = 0;

subjects.forEach(subject => {
for (let i = 0; i < subjects.length; i++) {
const subject = subjects[i];
Comment on lines +133 to +140

Copilot AI Apr 9, 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 changed but there are no unit tests covering statistics computation (avg/highest/total). Since this file already has tests for other helpers, consider adding a small DOM-based test (e.g., via JSDOM) to assert avgScore/highestScore updates for representative subject arrays, including large arrays and edge values.

Copilot uses AI. Check for mistakes.
const score = subject.scoreValue;

if (score > highest) {
highest = score;
}

Comment on lines +135 to +146

Copilot AI Apr 9, 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 via score > highest. If any subject.scoreValue is NaN (possible via getNumericScore returning Number(fallbackValue) without a NaN guard), comparisons will always be false and highest can remain -Infinity (or ignore NaNs), which changes behavior vs Math.max(...scores) (would produce NaN). Consider explicitly handling non-finite scores (e.g., track a hasValidScore/hasNaN flag and set the UI to '--' or propagate NaN consistently) so highestScore never renders -Infinity.

Copilot uses AI. Check for mistakes.
const weight = getSubjectWeight(subject.SubjectName);
totalWeightedScore += score * weight;
totalWeight += weight;
});
}
Comment on lines +139 to +150

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

For better readability and consistency with other parts of the codebase that use declarative iteration (e.g., forEach), consider using a for...of loop, as the index i is not used. This more clearly expresses the intent to iterate over the elements of the array.

Additionally, this change introduces a different behavior for the edge case where all scores are NaN. The previous Math.max(...scores) would result in NaN. With the new logic, highest will remain -Infinity and be rendered to the UI. This is likely not the desired outcome. You may want to handle this case after the loop by checking if highest is still -Infinity and setting it to a more user-friendly value like '--'.

    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