Created
April 9, 2025 14:47
-
-
Save hoadh/cb6cc5bf259e67546ef312483159e30b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Title</title> | |
</head> | |
<body> | |
<h1>Game Quiz Mini</h1> | |
<p id="question">Câu ?: Nội dung câu hỏi ở đây</p> | |
<button id="answer1" onclick="checkAnswer(0)" style="width: 200px;">Đáp án 1</button> | |
<br><br> | |
<button id="answer2" onclick="checkAnswer(1)" style="width: 200px;">Đáp án 2</button> | |
<br><br> | |
<button id="answer3" onclick="checkAnswer(2)" style="width: 200px;">Đáp án 3</button> | |
<br><br> | |
<button id="answer4" onclick="checkAnswer(3)" style="width: 200px;">Đáp án 4</button> | |
<br><br> | |
<button onclick="nextQuestion()" id="nextQuestion">Tiếp theo</button> | |
<p id="result"></p> | |
</body> | |
<script> | |
let questions = [ | |
"HTML viết tắt của từ gì?", // index = 0 | |
"Toán tử nào dùng để so sánh giá trị và kiểu?", // index = 1 | |
"JavaScript là ngôn ngữ gì?" // index = 2 | |
]; | |
let options = [ | |
[ "A", "B", "C", "D" ], // index = 0 | |
[ "E", "F", "G", "H" ], // index = 1 | |
[ "A", "B", "C", "D" ] // index = 2 | |
]; | |
// Khai báo biến đáp án đúng/sai (chỉ số đúng) | |
let correctAnswers = [ 2, 3, 0 ]; | |
let currentQuestion = 0; // Mặc định là câu hỏi đầu tiên | |
function renderQuestion() { | |
// Câu hỏi hiện tại là câu nào? | |
let currentText = questions[currentQuestion]; | |
let currentOptions = options[currentQuestion]; | |
document.getElementById("question").innerHTML = currentText; | |
document.getElementById("answer1").innerHTML = currentOptions[0]; | |
document.getElementById("answer2").innerHTML = currentOptions[1]; | |
document.getElementById("answer3").innerHTML = currentOptions[2]; | |
document.getElementById("answer4").innerHTML = currentOptions[3]; | |
} | |
function nextQuestion() { | |
// Làm sao để biết câu hỏi hiện tại là câu cuối cùng | |
if (currentQuestion < questions.length - 1) { | |
currentQuestion++; // chuyển qua câu hỏi tiếp theo | |
renderQuestion(); // rồi mới hiển thị câu hỏi | |
} else { // câu hỏi cuối cùng | |
// Hiển thị tổng điểm số | |
} | |
} | |
function checkAnswer(clickedIndex) { | |
// kiểm tra đáp án đúng hay sai. | |
let correctAnswer = correctAnswers[currentQuestion]; | |
if (clickedIndex == correctAnswer) { | |
document.getElementById("result").innerHTML = "Chính xác!"; | |
} else { | |
document.getElementById("result").innerHTML = "Sai rồi!"; | |
} | |
} | |
renderQuestion(); // đây là dòng lệnh sẽ hiển thị câu hỏi đầu tiên khi người dùng vào game. | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment