← Stopnja 2 – Čarovnik

Lekcija 6 od 8 ⏱ 40 minut

Kviz za tvoje obiskovalce

Vprašanja, odgovori in točkovanje. V miniaturni obliki se naučiš logike vsake igre.

Kako bo delovalo

Zgradil boš kviz: vprašanje, tri izbire, klik za odgovor in na koncu rezultat. Podatki kviza (vprašanja in odgovori) bodo živeli v polju objektov – zveni fancy, a vidiš, da je le urejeno organiziran seznam.

Pokažem ti: podatki kviza

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žem ti: motor kviza

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();

Novo:

Zdaj si na vrsti 💪

Zgradi kviz s lastnimi vprašanji – vsaj pet. Ideje za projekte:

Samo zamenjaj vsebino polja questions – motor kviza ostane enak. Zato sta podatki in logika ločena!

Bonus: barvna povratna informacija za odgovore

Želiš, da pravilen odgovor zasveti zeleno? Prilagodi answer, da najprej obarva gumbe, nato po sekundi pokaže naslednje vprašanje:

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) pomeni “to zaženi čez 1000 milisekund.” In style.backgroundColor spremeni CSS neposredno iz JavaScripta.

Preveri se: Kviz postavlja vprašanja ena za drugim, šteje točke in na koncu pokaže rezultat. Pravkar si napisal svojo prvo pravo aplikacijo – ima podatke, logiko in uporabniški vmesnik.

Kaj si vzameš iz te lekcije