Skip to content

Interactive Quiz App

Medium ~30 min

What you'll build

A quiz app that displays one question at a time with multiple-choice answers. The user selects an answer, clicks Next, and sees their score at the end with a restart option.

Goals

  • Manage quiz state (current question, score, selected answer)
  • Render questions and choices dynamically
  • Calculate and display the final score
  • Handle quiz restart

Prerequisites

  • useState
  • Conditional rendering
  • Array indexing

Steps

  1. 1

    Define quiz questions and state

    Create an array of question objects and state for current index, score, and selected answer.

    Code
    const questions = [
      { q: "What is JSX?", options: ["A framework", "A syntax extension", "A CSS tool", "A database"], answer: 1 },
      { q: "What does useState return?", options: ["A value", "An array", "An object", "A promise"], answer: 1 },
    ];
    const [current, setCurrent] = useState(0);
    const [score, setScore] = useState(0);
    const [selected, setSelected] = useState(null);
    const [finished, setFinished] = useState(false);

    Expected result

    Quiz data and state are ready.

  2. 2

    Render the current question

    Show the current question and its options as clickable buttons. Highlight the selected option.

    Code
    const question = questions[current];
    return (
      <div>
        <h2>{question.q}</h2>
        {question.options.map((opt, i) => (
          <button key={i} onClick={() => setSelected(i)}
            style={{ background: selected === i ? "lightblue" : "white" }}>
            {opt}
          </button>
        ))}
      </div>
    );

    Expected result

    Question and options are displayed. Clicking selects an answer.

  3. 3

    Handle the Next button and scoring

    Add a Next button that checks if the selected answer is correct, updates the score, and advances to the next question or finishes the quiz.

    Code
    const handleNext = () => {
      if (selected === question.answer) setScore(s => s + 1);
      if (current < questions.length - 1) {
        setCurrent(c => c + 1);
        setSelected(null);
      } else {
        setFinished(true);
      }
    };

    Expected result

    Next advances through questions and shows the score at the end.

  4. 4

    Show the final score and restart

    When finished is true, show the score and a restart button that resets all state.

    Code
    if (finished) {
      return (
        <div>
          <h2>Score: {score}/{questions.length}</h2>
          <button onClick={() => { setCurrent(0); setScore(0); setSelected(null); setFinished(false); }}>
            Restart
          </button>
        </div>
      );
    }

    Expected result

    Final score is displayed with a restart option.

  5. 5

    Add state management & event handling

    Use useState to manage component state and add onClick/onChange handlers to interactive elements. State is the heart of React components, and event handling lets them respond to user actions.

    Code
    import { useState } from "react";
    
    export function MyComponent() {
      const [count, setCount] = useState(0);
      const [text, setText] = useState("");
    
      return (
        <div>
          <button onClick={() => setCount(c => c + 1)}>
            Count: {count}
          </button>
          <input
            value={text}
            onChange={(e) => setText(e.target.value)}
          />
        </div>
      );
    }

    Expected result

    Clicking the button increments the count; the input content updates in real time as you type.

Try it yourself

What you learned

You built a complete interactive quiz with state management for navigation, scoring, and completion. This project ties together useState, conditional rendering, and event handling in a real application.

Related Resources

Related Lessons

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to topic