← Nivå 2 – Trollkarl

Lektion 6 av 8 ⏱ 40 minuter

Ett quiz för dina besökare

Frågor, svar och poängräkning. I miniatyr lär du dig logiken bakom varje spel.

Så funkar det

Du bygger ett quiz: en fråga, tre val, klick för att svara, och poängen visas i slutet. Quizdata (frågor och svar) bor i en array av objekt – det låter fancy, men det är bara en snyggt organiserad lista.

Jag visar: quizdata

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

Jag visar: quizmotorn

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

Nytt:

Nu är det din tur 💪

Bygg ett quiz med dina egna frågor – minst fem. Projektidéer:

Byt bara innehållet i questions-arrayen – quizmotorn är densamma. Det är precis därför data och logik hålls isär!

Bonus: färgad feedback på svar

Vill du att rätt svar blinkar grönt? Justera answer så den färgar knapparna först, sedan visar nästa fråga efter en sekund:

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) betyder “kör det här efter 1000 millisekunder.” Och style.backgroundColor ändrar CSS direkt från JavaScript.

Kolla själv: Quizet ställer frågor efter varandra, håller poäng och visar resultatet i slutet. Du skrev precis din första riktiga applikation – den har data, logik och ett användargränssnitt.

Det här tar du med dig från lektionen