-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaScript-based quiz application.html
More file actions
190 lines (179 loc) · 5.87 KB
/
Copy pathJavaScript-based quiz application.html
File metadata and controls
190 lines (179 loc) · 5.87 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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Quiz Application</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
text-align: center;
}
header {
background-color: #333;
color: white;
padding: 1rem;
}
header h1 {
margin: 0;
}
.container {
margin: 20px auto;
max-width: 600px;
padding: 20px;
border: 1px solid #ccc;
border-radius: 8px;
text-align: center;
}
.question {
font-size: 1.2rem;
margin-bottom: 20px;
}
.options button {
display: block;
width: 100%;
margin: 10px 0;
padding: 10px;
font-size: 1rem;
background-color: #333;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
.options button:hover {
background-color: #555;
}
.timer {
font-size: 1rem;
margin-bottom: 20px;
color: red;
}
.score {
font-size: 1.2rem;
margin: 20px 0;
}
.restart {
padding: 10px 20px;
font-size: 1rem;
background-color: #333;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
.restart:hover {
background-color: #555;
}
</style>
</head>
<body>
<header>
<h1>Quiz Application</h1>
</header>
<div class="container" id="quiz-container">
<div class="question" id="question">Loading...</div>
<div class="timer" id="timer">Time left: 10s</div>
<div class="options" id="options"></div>
<div class="score" id="score"></div>
<button class="restart" id="restart" style="display:none;">Restart Quiz</button>
</div>
<script>
const questions = [
{
question: "What is the capital of France?",
options: ["Berlin", "Madrid", "Paris", "Rome"],
correct: 2
},
{
question: "Which planet is known as the Red Planet?",
options: ["Earth", "Mars", "Jupiter", "Saturn"],
correct: 1
},
{
question: "Who wrote 'Hamlet'?",
options: ["Charles Dickens", "William Shakespeare", "Mark Twain", "Jane Austen"],
correct: 1
},
{
question: "What is the largest ocean on Earth?",
options: ["Atlantic Ocean", "Indian Ocean", "Pacific Ocean", "Arctic Ocean"],
correct: 2
},
{
question: "What is the square root of 64?",
options: ["6", "7", "8", "9"],
correct: 2
}
];
let currentQuestionIndex = 0;
let score = 0;
let timer;
const timerDuration = 10;
let timeLeft = timerDuration;
const quizContainer = document.getElementById('quiz-container');
const questionElement = document.getElementById('question');
const optionsElement = document.getElementById('options');
const timerElement = document.getElementById('timer');
const scoreElement = document.getElementById('score');
const restartButton = document.getElementById('restart');
function loadQuestion() {
if (currentQuestionIndex < questions.length) {
const currentQuestion = questions[currentQuestionIndex];
questionElement.textContent = currentQuestion.question;
optionsElement.innerHTML = '';
currentQuestion.options.forEach((option, index) => {
const button = document.createElement('button');
button.textContent = option;
button.addEventListener('click', () => selectAnswer(index));
optionsElement.appendChild(button);
});
resetTimer();
} else {
endQuiz();
}
}
function selectAnswer(selectedIndex) {
clearInterval(timer);
const currentQuestion = questions[currentQuestionIndex];
if (selectedIndex === currentQuestion.correct) {
score++;
}
currentQuestionIndex++;
loadQuestion();
}
function resetTimer() {
clearInterval(timer);
timeLeft = timerDuration;
timerElement.textContent = `Time left: ${timeLeft}s`;
timer = setInterval(() => {
timeLeft--;
timerElement.textContent = `Time left: ${timeLeft}s`;
if (timeLeft <= 0) {
clearInterval(timer);
currentQuestionIndex++;
loadQuestion();
}
}, 1000);
}
function endQuiz() {
questionElement.textContent = 'Quiz Over!';
optionsElement.innerHTML = '';
timerElement.textContent = '';
scoreElement.textContent = `Your final score is: ${score}/${questions.length}`;
restartButton.style.display = 'inline-block';
localStorage.setItem('quizScore', score);
}
restartButton.addEventListener('click', () => {
currentQuestionIndex = 0;
score = 0;
scoreElement.textContent = '';
restartButton.style.display = 'none';
loadQuestion();
});
loadQuestion();
</script>
</body>
</html>