-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathquize.js
73 lines (63 loc) · 2.22 KB
/
quize.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Quize Area
const questions = [
{
question: "In which year did Messi win his first Ballon d'Or?",
options: ["2008", "2009", "2010", "2011"],
answer: "2009"
},
{
question: "Which club did Messi join after leaving Barcelona in 2021?",
options: ["Manchester City", "PSG", "Inter Miami", "AC Milan"],
answer: "PSG"
},
{
question: "How many goals did Messi score in the 2022 FIFA World Cup?",
options: ["5", "6", "7", "8"],
answer: "7"
},
{
question: "Which country did Argentina defeat in the 2022 World Cup Final?",
options: ["France", "Brazil", "Germany", "Portugal"],
answer: "France"
}
];
let currentQuestionIndex = 0;
let score = 0;
const questionElement = document.querySelector(".question");
const optionsElement = document.querySelector(".options");
const resultElement = document.querySelector(".result");
function loadQuestion() {
const currentQuestion = questions[currentQuestionIndex];
questionElement.textContent = currentQuestion.question;
optionsElement.innerHTML = "";
currentQuestion.options.forEach(option => {
const button = document.createElement("button");
button.textContent = option;
button.onclick = () => checkAnswer(option);
optionsElement.appendChild(button);
});
}
function checkAnswer(selectedOption) {
const correctAnswer = questions[currentQuestionIndex].answer;
if (selectedOption === correctAnswer) {
score++;
resultElement.textContent = "✅ Correct!";
resultElement.style.color = "lightgreen";
} else {
resultElement.textContent = `❌ Wrong! The correct answer is ${correctAnswer}.`;
resultElement.style.color = "red";
}
document.querySelectorAll(".options button").forEach(btn => btn.disabled = true);
}
function nextQuestion() {
currentQuestionIndex++;
if (currentQuestionIndex < questions.length) {
resultElement.textContent = "";
loadQuestion();
} else {
questionElement.textContent = "Quiz Completed!";
optionsElement.innerHTML = "";
resultElement.innerHTML = `🎉 Your Score: ${score}/${questions.length}`;
}
}
loadQuestion();