-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
116 lines (100 loc) · 3.71 KB
/
script.js
File metadata and controls
116 lines (100 loc) · 3.71 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Quiz data: array of objects containing questions, options, and correct answers
const quizData = [
{
question: "What is the capital of France?",
options: ["Berlin", "Madrid", "Paris", "Rome"],
correct: "Paris"
},
{
question: "What is the capital of Latvia?",
options: ["Riga", "Helsinki", "Oslo", "Amsterdam"],
correct: "Riga"
},
{
question: "What is the capital of Bulgaria?",
options: ["Warsaw", "Bucharest", "Sofia", "London"],
correct: "Sofia"
},
{
question: "What is the capital of Italy?",
options: ["Athens", "Rome", "Milan", "Moscow"],
correct: "Rome"
}
];
// Variables to track the quiz state
let currentQuestionIndex = 0;
let score = 0;
// DOM elements
const questionElement = document.getElementById("question");
const optionsElement = document.getElementById("options");
const scoreElement = document.getElementById("score-value");
const nextButton = document.getElementById("next-btn");
const resultElement = document.getElementById("result");
// Load the first question
loadQuestion();
// Function to load the current question
function loadQuestion() {
const currentQuestion = quizData[currentQuestionIndex];
questionElement.textContent = currentQuestion.question;
// Clear previous options
optionsElement.innerHTML = "";
// Create buttons for each option
currentQuestion.options.forEach(option => {
const button = document.createElement("button");
button.textContent = option;
button.className = "w-full bg-blue-500 text-white py-2 rounded-lg hover:bg-blue-600 transition-colors";
// Add both click and touch support
button.addEventListener("click", () => selectOption(option));
button.addEventListener("touchstart", (e) => {
e.preventDefault(); // Prevent scrolling on touch
selectOption(option);
});
optionsElement.appendChild(button);
});
// Disable the Next button until an option is selected
nextButton.disabled = true;
nextButton.classList.add("opacity-50", "cursor-not-allowed");
}
// Function to handle option selection
function selectOption(selectedOption) {
const currentQuestion = quizData[currentQuestionIndex];
// Check if the selected option is correct
if (selectedOption === currentQuestion.correct) {
score++;
scoreElement.textContent = score;
}
// Highlight the correct and incorrect options
const optionButtons = optionsElement.getElementsByTagName("button");
for (let button of optionButtons) {
if (button.textContent === currentQuestion.correct) {
button.classList.remove("bg-blue-500", "hover:bg-blue-600");
button.classList.add("bg-green-500"); // Green for correct
} else if (button.textContent === selectedOption) {
button.classList.remove("bg-blue-500", "hover:bg-blue-600");
button.classList.add("bg-red-500"); // Red for incorrect
}
button.disabled = true; // Disable all buttons after selection
}
// Enable the Next button
nextButton.disabled = false;
nextButton.classList.remove("opacity-50", "cursor-not-allowed");
}
// Function to move to the next question or show the result
function nextQuestion() {
currentQuestionIndex++;
// Check if there are more questions
if (currentQuestionIndex < quizData.length) {
loadQuestion();
} else {
showResult();
}
}
// Function to show the final result
function showResult() {
questionElement.classList.add("hidden");
optionsElement.classList.add("hidden");
nextButton.classList.add("hidden");
resultElement.classList.remove("hidden");
resultElement.textContent = Quiz
}
// Complete! Your final score is ${score} out of ${quizData.length}.;