-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
96 lines (82 loc) · 2.51 KB
/
script.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Quiz data
const quizData = [
{
question: "What is the capital of France?",
options: ["Paris", "London", "Rome", "Berlin"],
correct: "Paris",
},
{
question: "Which programming language is known as the 'language of the web'?",
options: ["Python", "Java", "JavaScript", "C++"],
correct: "JavaScript",
},
{
question: "What is 5 + 3?",
options: ["5", "8", "12", "7"],
correct: "8",
},
];
let currentQuestionIndex = 0;
let score = 0;
// DOM elements
const questionElement = document.getElementById("question");
const optionsContainer = document.getElementById("options");
const nextButton = document.getElementById("next-button");
const resultContainer = document.getElementById("result-container");
const resultElement = document.getElementById("result");
// Display the current question
function loadQuestion() {
const currentQuestion = quizData[currentQuestionIndex];
questionElement.textContent = currentQuestion.question;
// Clear previous options
optionsContainer.innerHTML = "";
// Display options
currentQuestion.options.forEach((option) => {
const button = document.createElement("button");
button.textContent = option;
button.classList.add("option-button");
button.onclick = () => selectAnswer(button, option);
optionsContainer.appendChild(button);
});
nextButton.disabled = true;
}
// Handle answer selection
function selectAnswer(button, option) {
const correctAnswer = quizData[currentQuestionIndex].correct;
// Highlight correct and incorrect answers
if (option === correctAnswer) {
button.style.backgroundColor = "green";
score++;
} else {
button.style.backgroundColor = "red";
Array.from(optionsContainer.children).forEach((btn) => {
if (btn.textContent === correctAnswer) {
btn.style.backgroundColor = "green";
}
});
}
// Disable all buttons after selecting an answer
Array.from(optionsContainer.children).forEach((btn) => {
btn.disabled = true;
});
nextButton.disabled = false;
}
// Move to the next question
function nextQuestion() {
currentQuestionIndex++;
if (currentQuestionIndex < quizData.length) {
loadQuestion();
} else {
showResult();
}
}
// Display the result
function showResult() {
questionElement.style.display = "none";
optionsContainer.style.display = "none";
nextButton.style.display = "none";
resultContainer.style.display = "block";
resultElement.textContent = `You scored ${score} out of ${quizData.length}!`;
}
// Initialize the quiz
loadQuestion();