← Level 2 – Wizard

Pelajaran 6 dari 8 ⏱ 40 menit

Kuis untuk pengunjung kamu

Pertanyaan, jawaban, dan pencatatan skor. Secara singkat, kamu akan mempelajari logika di balik setiap permainan.

Bagaimana cara kerjanya

kamu akan membuat kuis: satu pertanyaan, tiga pilihan, klik untuk menjawab, dan skor muncul di akhir. Data kuis (pertanyaan dan jawaban) akan disimpan dalam array objek – kedengarannya mewah, tetapi kamu akan melihat bahwa itu hanyalah daftar yang tertata rapi.

Tunjukkan pada saya: data kuis

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

Tunjukkan pada saya: mesin kuis

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

Barang baru:

Giliran kamu 💪

Buat kuis dengan pertanyaan kamu sendiri – setidaknya lima. Ide proyek:

Cukup tukar isi array pertanyaan – mesin kuis tetap sama. Itulah sebabnya data dan logika dipisahkan!

Bonus: masukan berwarna untuk jawaban

Ingin jawaban yang benar berkedip hijau? Sesuaikan jawaban sehingga mewarnai tombol terlebih dahulu, lalu menampilkan pertanyaan berikutnya setelah beberapa saat:

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) berarti “jalankan ini setelah 1000 milidetik.” Dan style.backgroundColor mengubah CSS langsung dari JavaScript.

Periksa diri kamu: Kuis menanyakan pertanyaan satu demi satu, mencatat skor, dan menampilkan hasilnya di akhir. kamu baru saja menulis aplikasi nyata pertama kamu – aplikasi tersebut memiliki data, logika, dan antarmuka pengguna.

Apa yang bisa diambil dari pelajaran ini