Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

<!

DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Quiz Game</title>
<script>
// JavaScript code
let questions = [
{ question: "What is the capital of France?", options: ["Paris", "London",
"Rome", "Berlin"], answer: "Paris" },
{ question: "What is 2 + 2?", options: ["3", "4", "5", "6"], answer: "4" },
// Add more questions as needed
];
let currentQuestionIndex = 0;
let score = 0;

function displayQuestion() {
let question = questions[currentQuestionIndex];
document.getElementById('question').textContent = question.question;
let optionsHtml = '';
for (let i = 0; i < question.options.length; i++) {
optionsHtml += `<button onclick="checkAnswer('${question.options[i]}')">$
{question.options[i]}</button>`;
}
document.getElementById('options').innerHTML = optionsHtml;
}

function checkAnswer(selectedOption) {
let correctAnswer = questions[currentQuestionIndex].answer;
if (selectedOption === correctAnswer) {
score++;
alert('Correct!');
} else {
alert('Wrong answer. The correct answer was ' + correctAnswer);
}
currentQuestionIndex++;
if (currentQuestionIndex >= questions.length) {
endGame();
} else {
displayQuestion();
}
}

function endGame() {
document.getElementById('game').innerHTML = `<p>Your score is ${score}/$
{questions.length}</p>`;
}

window.onload = displayQuestion;
</script>
</head>
<body>
<div id="game">
<p id="question"></p>
<div id="options"></div>
</div>
</body>
</html>

You might also like