Skip to content

Feature/fixed quiz scoring#56

Open
Prag-18 wants to merge 3 commits into
AditixAnand:mainfrom
Prag-18:feature/fixed-quiz-scoring
Open

Feature/fixed quiz scoring#56
Prag-18 wants to merge 3 commits into
AditixAnand:mainfrom
Prag-18:feature/fixed-quiz-scoring

Conversation

@Prag-18
Copy link
Copy Markdown

@Prag-18 Prag-18 commented May 17, 2026

Description

This PR resolves an issue in the quiz scoring system (scripts/quiz.js) where the user's score could be artificially inflated. Previously, clicking a correct option repeatedly or toggling between options would continuously increment the score, allowing it to exceed the total number of questions in the quiz.

To fix this, I introduced a robust state-tracking mechanism using a userAnswers object to map each question to its selected answer. The score is now dynamically recalculated whenever an answer changes, ensuring score consistency and preventing duplicate increments.


🛠️ Changes Made

Resolved #16 Quiz Score Double-Counts on Option Re-Select

Added Answer Tracking State

  • Introduced a userAnswers state object to store selections in the format:
    { questionIndex: chosenOptionIndex }

Reset State on Quiz Start

  • Initialized and cleared userAnswers inside the startQuiz() function to ensure a clean state for every new quiz session.

Dynamic Score Recalculation

  • Refactored selectOption() to:

    • Save the selected option in userAnswers
    • Recompute the score based on all recorded answers
    • Prevent score inflation caused by repeated clicks

Visual Answer Persistence

  • Updated showQuestion() to:

    • Restore previously selected answers from userAnswers
    • Keep selections visually highlighted when navigating using the Next and Prev buttons

Prevented Redundant Operations

  • Added an early-return guard in selectOption() when the user clicks an already selected option.

🧪 How to Test / Reproduce

  1. Start any quiz (e.g., Web Development Quiz).
  2. Click the correct option for Question 1 multiple times.

✅ Expected Behavior

  • The score increases by exactly 1 on the first click.
  • Repeated clicks on the same correct option do not increase the score further.
  1. Select an incorrect option after previously selecting the correct option.

✅ Expected Behavior

  • The score decreases correctly by 1.
  1. Navigate to the next question, make a selection, then return using the Prev button.

✅ Expected Behavior

  • The previously selected option remains restored and visually highlighted.

✅ Result

  • Prevents score manipulation
  • Ensures accurate score calculation
  • Preserves user selections during navigation
  • Improves overall quiz reliability and user experience

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.

Issue #11 — Quiz Score Double-Counts on Option Re-Select

1 participant