← Poziom 2 – Czarodziej

Lekcja 6 z 8 ⏱ 40 minut

Quiz dla Twoich gości

Pytania, odpowiedzi i liczenie punktów. W miniaturze poznasz logikę każdej gry.

Jakie są zasady działania?

Stworzysz quiz: pytanie, trzy odpowiedzi, kliknij, aby odpowiedzieć, a wynik pojawi się na końcu. Dane quizu (pytania i odpowiedzi) będą mieszkać w tablicy obiektów – to brzmi fantazyjnie, ale zobaczysz, że jest to tylko zgrabnie zorganizowana lista.

Pokażę ci: quiz data

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,
  },
];

Pokażę ci: 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();

Thanks.

Teraz ty 💪

Stwórz quiz zwłasnymipytaniami – co najmniej pięć. Pomysły na projekty:

Wystarczy zamienić zawartośćquestionstablica – silnik quizu pozostaje taki sam. Właśnie dlatego dane i logika są oddzielone!

Bonus: colored feedback for answers

Chcesz uzyskać prawidłową odpowiedź, aby migać na zielono? Dostosujanswerwięc najpierw koloruje przyciski, a następnie wyświetla następne pytanie po sekundzie:

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)oznacza „uruchom to po 1000 milisekundach”. Istyle.backgroundColorzmienia CSS bezpośrednio z JavaScript.

Check yourself: The quiz asks questions one after another, keeps score, and shows the result at the end. You just wrote your first real application – it has data, logic, and a user interface.

Co wyniesiesz z tej lekcji