-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddition.html
323 lines (278 loc) · 12 KB
/
addition.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Complement of Addition Quiz</title>
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
.timer {
font-size: 1.5rem;
color: red;
}
.wrong-answer {
color: red;
}
/* Celebration Animation */
.celebration {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 9999;
background: rgba(0, 0, 0, 0.6);
display: flex;
justify-content: center;
align-items: center;
color: #fff;
font-size: 2rem;
font-weight: bold;
animation: fadeIn 1s ease;
}
@keyframes fadeIn {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
.confetti {
position: fixed;
top: 0;
left: 0;
width: 100vw;
/* Full viewport width */
height: 100vh;
/* Full viewport height */
pointer-events: none;
/* Allow interaction with elements behind confetti */
z-index: 9999;
/* Ensure it's on top */
}
.confetti-piece {
position: absolute;
width: 10px;
height: 10px;
background-color: random;
opacity: 0.9;
border-radius: 50%;
animation: fall 3s linear infinite;
}
@keyframes fall {
0% {
transform: translateY(0) translateX(0);
/* Start at top */
}
100% {
transform: translateY(100vh) translateX(calc(50vw - 50%));
/* End at bottom */
}
}
</style>
</head>
<body class="bg-light">
<div class="container my-5">
<div class="text-center">
<h1 class="mb-4">Complement of Addition Quiz</h1>
</div>
<!-- Settings Section -->
<div id="settings" class="card p-4 shadow-sm mb-4">
<h2 class="mb-3">Quiz Settings</h2>
<div class="mb-3">
<label for="complementNumber" class="form-label">Complement Number:</label>
<input type="number" id="complementNumber" class="form-control" min="1" value="7">
</div>
<div class="mb-3">
<label for="numQuestions" class="form-label">Number of Questions:</label>
<input type="number" id="numQuestions" class="form-control" min="1" value="20">
</div>
<div class="mb-3">
<label for="timerDuration" class="form-label">Timer (seconds):</label>
<input type="number" id="timerDuration" class="form-control" min="10" value="60">
</div>
<button class="btn btn-primary w-100" onclick="startQuiz()">Start Quiz</button>
</div>
<!-- Quiz Section -->
<div id="quiz" class="card p-4 shadow-sm mb-4 visually-hidden">
<h2 class="mb-3">Quiz</h2>
<div id="questions" class="mb-3"></div>
<div class="timer mb-3" id="timer">Time Left: 60s</div>
<button id="submit" class="btn btn-success w-100" onclick="submitQuiz(false)">Submit</button>
</div>
<!-- Result Section -->
<div id="result" class="card p-4 shadow-sm mb-4 visually-hidden">
<h2>Quiz Results</h2>
</div>
<!-- History Section -->
<div id="history" class="card p-4 shadow-sm">
<h2>Latest Results</h2>
<ul id="historyList" class="list-group"></ul>
</div>
<!-- Celebration Container -->
<div id="celebration" class="celebration visually-hidden">
🎉 Congratulations! You got everything correct! 🎉
</div>
<div class="confetti" id="confetti"></div>
</div>
<!-- Bootstrap JS Bundle -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script>
const settingsDiv = document.getElementById('settings');
const quizDiv = document.getElementById('quiz');
const questionsDiv = document.getElementById('questions');
const timerElement = document.getElementById('timer');
const resultDiv = document.getElementById('result');
const historyList = document.getElementById('historyList');
let complementNumber;
let totalQuestions;
let questions = [];
let timerInterval;
let timeLeft;
let timerDuration;
function loadHistory() {
const history = JSON.parse(localStorage.getItem('additionHistory')) || [];
historyList.innerHTML = history.map(
(entry, index) =>
`<li class="list-group-item">${index + 1}.
Score: ${entry.score}/${entry.totalQuestions},
Timer: ${entry.timerDuration}s,
Time Left: ${entry.timeLeft}s,
Submitted: ${entry.submittedAutomatically ? "Automatically (Timer Ended)" : "Manually"},
Date: ${entry.date}</li>`
).join('');
}
function saveToHistory(score, totalQuestions, submittedAutomatically, timeLeft) {
const history = JSON.parse(localStorage.getItem('additionHistory')) || [];
const newEntry = {
score,
totalQuestions,
timerDuration,
timeLeft,
submittedAutomatically,
date: new Date().toLocaleString()
};
history.unshift(newEntry); // Add new entry at the beginning
if (history.length > 5) history.pop(); // Keep only the latest 5 entries
localStorage.setItem('additionHistory', JSON.stringify(history));
}
// Confetti Generator
function generateConfetti() {
const confettiContainer = document.getElementById('confetti');
confettiContainer.innerHTML = ''; // Clear any previous confetti
for (let i = 0; i < 100; i++) {
const confettiPiece = document.createElement('div');
confettiPiece.classList.add('confetti-piece');
// Randomize the starting position, size, and fall speed
const startX = Math.random() * 100; // Random starting position (percent of screen width)
const startY = Math.random() * 50; // Random starting height (top half of screen)
const size = 5 + Math.random() * 10; // Random size for variety
const duration = 3 + Math.random() * 3; // Duration between 3 and 6 seconds
const delay = Math.random() * 2; // Random delay (0-2 seconds)
confettiPiece.style.left = `${startX}vw`; // Random horizontal start position
confettiPiece.style.top = `${startY}vh`; // Random vertical start position
confettiPiece.style.width = `${size}px`; // Random width
confettiPiece.style.height = `${size}px`; // Random height
confettiPiece.style.backgroundColor = `hsl(${Math.random() * 360}, 100%, 50%)`; // Random color
// Apply animation for falling and spinning
confettiPiece.style.animation = `fall ${duration}s linear ${delay}s infinite`;
confettiContainer.appendChild(confettiPiece);
}
}
function startCelebration() {
const celebrationDiv = document.getElementById('celebration');
const confettiContainer = document.getElementById('confetti');
celebrationDiv.classList.remove('visually-hidden');
generateConfetti();
setTimeout(() => {
celebrationDiv.classList.add('visually-hidden');
confettiContainer.innerHTML = ''; // Clear confetti after celebration
}, 5000);
}
function generateQuestions() {
questions = [];
for (let i = 0; i < totalQuestions; i++) {
const knownNumber = Math.floor(Math.random() * complementNumber);
const equation = `${knownNumber} + ... = ${complementNumber}`;
const answer = complementNumber - knownNumber;
questions.push({ equation, answer });
}
}
function renderQuiz() {
questionsDiv.innerHTML = ''; // Clear previous questions
questions.forEach((q, index) => {
const questionElement = document.createElement('div');
questionElement.classList.add('mb-3');
questionElement.innerHTML = `
<label for="q${index}" class="form-label">${q.equation}</label>
<input type="number" id="q${index}" class="form-control" data-answer="${q.answer}">
`;
questionsDiv.appendChild(questionElement);
});
}
function startTimer() {
timerElement.textContent = `Time Left: ${timeLeft}s`;
timerInterval = setInterval(() => {
timeLeft--;
timerElement.textContent = `Time Left: ${timeLeft}s`;
if (timeLeft <= 0) {
clearInterval(timerInterval);
submitQuiz(true); // Automatically submit when timer ends
}
}, 1000);
}
function startQuiz() {
complementNumber = parseInt(document.getElementById('complementNumber').value, 10);
totalQuestions = parseInt(document.getElementById('numQuestions').value, 10);
timeLeft = parseInt(document.getElementById('timerDuration').value, 10);
timerDuration = timeLeft;
if (complementNumber < 1 || totalQuestions < 1 || timeLeft < 10) {
alert('Please enter valid values for the complement number, number of questions, and timer duration.');
return;
}
settingsDiv.classList.add('visually-hidden');
quizDiv.classList.remove('visually-hidden');
resultDiv.classList.add('visually-hidden');
generateQuestions();
renderQuiz();
startTimer();
}
function submitQuiz(submittedAutomatically) {
clearInterval(timerInterval);
const inputs = document.querySelectorAll('#questions input');
let score = 0;
const feedback = [];
inputs.forEach((input, index) => {
const correctAnswer = parseInt(input.dataset.answer, 10);
const userAnswer = parseInt(input.value, 10);
if (userAnswer === correctAnswer) {
score++;
} else {
feedback.push(
`<p class="wrong-answer">Q${index + 1}: ${questions[index].equation} (Your answer: ${userAnswer || "No answer"}, Correct: ${correctAnswer})</p>`
);
}
});
quizDiv.classList.add('visually-hidden');
resultDiv.classList.remove('visually-hidden');
resultDiv.innerHTML = `
<h2>Your Score: ${score}/${totalQuestions}</h2>
${feedback.length > 0 ? `<h3 class="mt-3">Incorrect Answers:</h3>${feedback.join('')}` : '<p class="text-success mt-3">Great job! All answers are correct.</p>'}
`;
// Trigger Celebration Animation
if (score === totalQuestions) {
startCelebration();
}
saveToHistory(score, totalQuestions, submittedAutomatically, timeLeft);
loadHistory();
if (submittedAutomatically) {
alert('Time is up! Your quiz has been submitted automatically.');
}
}
// Load history on page load
loadHistory();
</script>
</body>
</html>