Bài học hiện đang bằng tiếng Séc. Giao diện đã được dịch – nội dung bài học sẽ sớm có.

← Cấp 2 – Pháp sư

Bài 6 / 8 ⏱ 40 phút

Câu đố cho khách truy cập

Câu hỏi, đáp án và tính điểm. Thu nhỏ, bạn học logic đằng sau mọi game.

Cách hoạt động

Bạn sẽ làm câu đố: một câu hỏi, ba lựa chọn, bấm để trả lời, và điểm hiện ở cuối. Dữ liệu câu đố (câu hỏi và đáp án) nằm trong mảng object – nghe cao siêu, nhưng thực ra chỉ là danh sách gọn gàng.

Xem này: dữ liệu câu đố

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

Xem này: động cơ câu đố

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

Thứ mới:

Đến lượt bạn 💪

Làm câu đố với câu hỏi riêng – ít nhất năm câu. Ý tưởng theo dự án:

Chỉ cần đổi nội dung mảng questions – động cơ câu đố giữ nguyên. Đó chính là lý do tách dữ liệu và logic!

Thêm: phản hồi màu cho đáp án

Muốn đáp án đúng nhấp nháy xanh? Sửa answer để tô màu nút trước, rồi hiện câu tiếp sau một giây:

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) nghĩa là “chạy cái này sau 1000 mili giây.” Và style.backgroundColor đổi CSS trực tiếp từ JavaScript.

✅

Tự kiểm tra: Câu đố hỏi lần lượt, tính điểm, và hiện kết quả ở cuối. Bạn vừa viết ứng dụng thật đầu tiên – có dữ liệu, logic và giao diện.

Điều cần nhớ từ bài học này