-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
167 lines (143 loc) · 5.24 KB
/
index.html
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Quiz App</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
padding: 20px;
border: 1px solid #ccc;
border-radius: 5px;
max-width: 400px;
}
.question {
margin: 20px 0;
}
.result {
margin-top: 20px;
font-weight: bold;
}
.hidden {
display: none;
}
.correct {
background-color: lightgreen;
}
.incorrect {
background-color: lightcoral;
}
button {
margin-top: 10px;
}
</style>
</head>
<body>
<h1>AI Version</h1>
<form id="quiz-form">
<div id="question-container"></div>
<button type="button" id="next-button" class="hidden">Next Question</button>
<div id="result" class="result hidden"></div>
</form>
<br><br>
<a href="style.html" class="link">See my Version</a>
<script>
const questions = [
{
question: "What is the capital of France?",
answers: { A: "Berlin", B: "Paris", C: "Madrid" },
correct: "B"
},
{
question: "Who wrote 'Hamlet'?",
answers: { A: "Charles Dickens", B: "William Shakespeare", C: "Leo Tolstoy" },
correct: "B"
},
{
question: "What is the largest planet in our Solar System?",
answers: { A: "Earth", B: "Jupiter", C: "Saturn" },
correct: "B"
},
{
question: "What is the chemical symbol for water?",
answers: { A: "O2", B: "H2O", C: "CO2" },
correct: "B"
},
{
question: "Who painted the Mona Lisa?",
answers: { A: "Vincent Van Gogh", B: "Pablo Picasso", C: "Leonardo da Vinci" },
correct: "C"
}
];
let currentQuestionIndex = 0;
let score = 0;
function loadQuestion() {
const questionContainer = document.getElementById("question-container");
questionContainer.innerHTML = "";
const currentQuestion = questions[currentQuestionIndex];
const questionElement = document.createElement("div");
questionElement.className = "question";
questionElement.innerHTML = `<p>${currentQuestionIndex + 1}. ${currentQuestion.question}</p>`;
for (const [key, value] of Object.entries(currentQuestion.answers)) {
questionElement.innerHTML += `
<label>
<input type="radio" name="answer" value="${key}">
${key}) ${value}
</label><br>
`;
}
questionContainer.appendChild(questionElement);
}
function validateAnswer(selectedAnswer) {
const currentQuestion = questions[currentQuestionIndex];
const correctAnswer = currentQuestion.correct;
if (selectedAnswer === correctAnswer) {
score++;
highlightAnswer(selectedAnswer, true);
} else {
highlightAnswer(selectedAnswer, false);
highlightAnswer(correctAnswer, true);
}
// Show the next button
document.getElementById("next-button").classList.remove("hidden");
}
function highlightAnswer(answer, isCorrect) {
const labels = document.querySelectorAll(`input[name="answer"]`);
labels.forEach(label => {
if (label.value === answer) {
label.parentElement.classList.add(isCorrect ? "correct" : "incorrect");
}
});
}
document.getElementById("quiz-form").addEventListener("change", (e) => {
if (e.target.name === "answer") {
validateAnswer(e.target.value);
// Disable further selection until the answer is validated
document.querySelectorAll('input[name="answer"]').forEach(input => {
input.disabled = true;
});
}
});
document.getElementById("next-button").addEventListener("click", () => {
currentQuestionIndex++;
if (currentQuestionIndex < questions.length) {
loadQuestion();
document.getElementById("next-button").classList.add("hidden");
} else {
displayResult();
}
});
function displayResult() {
const resultDiv = document.getElementById("result");
resultDiv.textContent = `Your total score is ${score} out of ${questions.length}.`;
resultDiv.classList.remove("hidden");
document.getElementById("question-container").classList.add("hidden");
document.getElementById("next-button").classList.add("hidden");
}
// Load the first question
loadQuestion();
</script>
</body>
</html>