Skip to content

Fix Quiz Score Bug#1

Open
google-labs-jules[bot] wants to merge 1 commit intomainfrom
fix-quiz-score-bug
Open

Fix Quiz Score Bug#1
google-labs-jules[bot] wants to merge 1 commit intomainfrom
fix-quiz-score-bug

Conversation

@google-labs-jules
Copy link

The quiz score could previously exceed 100% because the count of correct answers was stored in a state variable that could be incremented multiple times for the same question if the user navigated back and forth. This change fixes the bug by removing the correctAnswers state and calculating the count dynamically on each render from the userAnswers array. This ensures the score is always derived from a single source of truth and cannot be incorrectly inflated.

@dangerworm dangerworm requested a review from Copilot August 31, 2025 20:08
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes a bug where quiz scores could exceed 100% due to a state variable that could be incorrectly incremented when users navigated between questions. The fix replaces the stateful correctAnswers counter with a dynamically calculated value based on the userAnswers array.

  • Removes the correctAnswers state variable and its related setters
  • Adds a correctAnswersCount calculation that derives the count from userAnswers on each render
  • Updates the props passed to components to use the new calculated value

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +207 to +213
const correctAnswersCount = userAnswers.reduce((count, answers, index) => {
const question = randomizedQuestions[index];
if (answers && answers.length > 0 && question && checkAnswer(answers, question)) {
return count + 1;
}
return count;
}, 0);
Copy link

Copilot AI Aug 31, 2025

Choose a reason for hiding this comment

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

The correctAnswersCount calculation runs on every render, which could be inefficient for large quiz sets. Consider using useMemo with dependencies on userAnswers and randomizedQuestions to optimize this calculation.

Copilot uses AI. Check for mistakes.
@dangerworm dangerworm marked this pull request as ready for review August 31, 2025 20:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant