18 - Interactive Quiz with Dynamic Results and Feedback

Description

Develop an interactive quiz where users answer questions and receive:

  • Dynamic results based on their answers.

  • Personalized feedback tailored to their choices.

  • Optional features:

    • Progress tracking throughout the quiz.

    • Multiple attempts with different results.

    • Leaderboards for comparing scores.

Algorithm

  1. Data Model:

    • Define a data structure for questions (e.g., object array). Each question contains:

      • Text and answer options.

      • Scoring logic (points for each answer).

      • Feedback messages for different answer choices.

  2. User Interface:

    • Display questions one by one.

    • Present answer options (e.g., radio buttons, checkboxes).

    • Offer a submission button to check answers.

    • Show results and personalized feedback after question submission.

  3. Interactive Logic:

    • Track user selections for each question.

    • Calculate score based on scoring logic in the data model.

    • Show dynamic results based on the calculated score.

    • Fetch and display relevant feedback messages for chosen answers.

Code

interface Question {
  text: string;
  answers: { text: string; points: number; feedback: string }[];
}

function InteractiveQuiz() {
  const [questions, setQuestions] = useState<Question[]>([]);
  const [currentQuestion, setCurrentQuestion] = useState(0);
  const [userAnswers, setUserAnswers] = useState({}[number]);
  const [score, setScore] = useState(0);

  useEffect(() => {
    // Fetch quiz data and set questions state
  }, []);

  const handleAnswerClick = (questionIndex: number, answerIndex: number) => {
    setUserAnswers({ ...userAnswers, [questionIndex]: answerIndex });
  };

  const handleSubmit = () => {
    const question = questions[currentQuestion];
    const answerPoints = question.answers[userAnswers[currentQuestion]].points;
    setScore(score + answerPoints);
    // Show results and feedback based on score and user answers
  };

  return (
    <div className="quiz">
      {questions.length > 0 && (
        <div>
          <p>{questions[currentQuestion].text}</p>
          {questions[currentQuestion].answers.map((answer, index) => (
            <button key={index} onClick={() => handleAnswerClick(currentQuestion, index)}>
              {answer.text}
            </button>
          ))}
          <button onClick={handleSubmit}>Submit</button>
          {/* Show results and feedback here based on state */}
        </div>
      )}
    </div>
  );
}

Explanation

  • questions state holds the quiz data array of question objects.

  • currentQuestion tracks the current question index.

  • userAnswers stores user selections for each question.

  • score reflects the user's accumulated points.

  • handleAnswerClick updates user answer for a specific question.

  • handleSubmit calculates score and triggers result/feedback display.

Additional Notes

  • Consider using a state management library like Redux for complex quiz logic and data handling.

  • Implement validation for user answers and handle edge cases.

  • Design engaging result and feedback messages for different scenarios.

  • Explore integrating multimedia elements like images or audio into questions.

Last updated