← Livello 2 – Mago

Lezione 6 di 8 ⏱ 40 minuti

Un quiz per i tuoi visitatori

Domande, risposte e punteggio. In miniatura imparerai la logica dietro ogni gioco.

Come funzionerà

Costruirai un quiz: una domanda, tre scelte, clic per rispondere, e il punteggio alla fine. I dati del quiz (domande e risposte) vivranno in un array di oggetti – suona complicato, ma vedrai che è solo una lista ordinata.

Te lo mostro: i dati del quiz

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

Te lo mostro: il motore del quiz

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

Novità:

Ora tocca a te 💪

Costruisci un quiz con tue domande – almeno cinque. Idee per progetto:

Basta cambiare il contenuto dell’array questions – il motore del quiz resta uguale. È esattamente per questo che dati e logica sono separati!

Bonus: feedback colorato per le risposte

Vuoi che la risposta giusta lampeggi in verde? Modifica answer così colora i pulsanti prima, poi mostra la prossima domanda dopo un secondo:

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) significa “esegui questo dopo 1000 millisecondi.” E style.backgroundColor cambia il CSS direttamente da JavaScript.

Controlla da solo: Il quiz fa domande una dopo l’altra, tiene il punteggio e mostra il risultato alla fine. Hai appena scritto la tua prima applicazione vera – ha dati, logica e interfaccia utente.

Cosa porti via da questa lezione