Open
Conversation
There was a problem hiding this comment.
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
correctAnswersstate variable and its related setters - Adds a
correctAnswersCountcalculation that derives the count fromuserAnswerson 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); |
There was a problem hiding this comment.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
correctAnswersstate and calculating the count dynamically on each render from theuserAnswersarray. This ensures the score is always derived from a single source of truth and cannot be incorrectly inflated.