← Level 2 – Zauberer

Lektion 6 von 8 ⏱ 40 Minuten

Ein Quiz für deine Besucher

Fragen, Antworten und Punktezählung. Im Miniaturformat lernst du die Logik hinter jedem Spiel.

Wie es funktionieren wird

Du baust ein Quiz: eine Frage, drei Antwortmöglichkeiten, Klick zum Antworten, und am Ende erscheint der Punktestand. Die Quiz-Daten (Fragen und Antworten) leben in einem Array von Objekten – das klingt fancy, aber du wirst sehen, es ist nur eine ordentlich organisierte Liste.

Zeig mir das: die Quiz-Daten

const questions = [
  {
    text: "How many legs does a spider have?",
    choices: ["6", "8", "10"],
    correct: 1,
  },
  {
    text: "Which animal is the fastest?",
    choices: ["cheetah", "falcon", "hare"],
    correct: 1,
  },
  {
    text: "What's the name of our website?",
    choices: ["Code with Martin", "Code with Charlie", "Web with Wendy"],
    correct: 0,
  },
];

Zeig mir das: die Quiz-Engine

HTML:

<div class="card">
  <h2 id="quiz-question"></h2>
  <div id="quiz-choices"></div>
  <p id="quiz-status"></p>
</div>

JavaScript:

const questionEl = document.getElementById("quiz-question");
const choicesEl = document.getElementById("quiz-choices");
const statusEl = document.getElementById("quiz-status");

let questionNumber = 0;
let points = 0;

function showQuestion() {
  const question = questions[questionNumber];
  questionEl.textContent = question.text;
  choicesEl.innerHTML = "";   // clear the old buttons

  question.choices.forEach((choice, index) => {
    const button = document.createElement("button");
    button.textContent = choice;
    button.addEventListener("click", () => answer(index));
    choicesEl.appendChild(button);
  });

  statusEl.textContent = "Question " + (questionNumber + 1) + " of " + questions.length;
}

function answer(index) {
  if (index === questions[questionNumber].correct) {
    points++;
  }

  questionNumber++;

  if (questionNumber < questions.length) {
    showQuestion();
  } else {
    questionEl.textContent = "Done! 🎉";
    choicesEl.innerHTML = "";
    statusEl.textContent = "You scored " + points + " out of " + questions.length + " points.";
  }
}

showQuestion();

Neue Sachen:

Jetzt du 💪

Baue ein Quiz mit deinen eigenen Fragen – mindestens fünf. Projektideen:

Tausch einfach den Inhalt des questions-Arrays aus – die Quiz-Engine bleibt gleich. Genau deshalb werden Daten und Logik getrennt gehalten!

Bonus: farbiges Feedback für Antworten

Soll die richtige Antwort grün aufleuchten? Passe answer so an, dass sie zuerst die Buttons einfärbt und dann nach einer Sekunde die nächste Frage zeigt:

function answer(index) {
  const buttons = choicesEl.querySelectorAll("button");
  const correct = questions[questionNumber].correct;

  buttons[correct].style.backgroundColor = "#16a34a";
  if (index !== correct) {
    buttons[index].style.backgroundColor = "#dc2626";
  } else {
    points++;
  }

  setTimeout(() => {
    questionNumber++;
    if (questionNumber < questions.length) {
      showQuestion();
    } else {
      questionEl.textContent = "Done! 🎉";
      choicesEl.innerHTML = "";
      statusEl.textContent = "You scored " + points + " out of " + questions.length + " points.";
    }
  }, 1000);
}

setTimeout(() => {...}, 1000) bedeutet „führe das nach 1000 Millisekunden aus.” Und style.backgroundColor ändert CSS direkt aus JavaScript.

Kontrolliere dich selbst: Das Quiz stellt Fragen nacheinander, zählt Punkte und zeigt am Ende das Ergebnis. Du hast gerade deine erste echte Anwendung geschrieben – sie hat Daten, Logik und eine Benutzeroberfläche.

Das nimmst du aus dieser Lektion mit