-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
357 lines (296 loc) · 14.4 KB
/
script.js
File metadata and controls
357 lines (296 loc) · 14.4 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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
let currentQuizData = [
{
question: "Which language runs in a web browser?",
a: "Java",
b: "C",
c: "Python",
d: "JavaScript",
correct: "d"
},
{
question: "What does CSS stand for?",
a: "Central Style Sheets",
b: "Cascading Style Sheets",
c: "Cascading Simple Sheets",
d: "Cars SUVs Sailboats",
correct: "b"
},
{
question: "What does HTML stand for?",
a: "Hypertext Markup Language",
b: "Hypertext Markdown Language",
c: "Hyperloop Machine Language",
d: "Helicopters Terminals Motorboats Lamborginis",
correct: "a"
},
{
question: "What year was JavaScript launched?",
a: "1996",
b: "1995",
c: "1994",
d: "none of the above",
correct: "b"
}
];
let currentQuizTitle = "Sample Quiz";
let currentQuestionIndex = 0;
let score = 0;
let userAnswers = [];
// Tab switching functionality
function switchTab(tabName) {
// Remove active class from all tabs
document.querySelectorAll('.tab-button').forEach(btn => btn.classList.remove('active'));
document.querySelectorAll('.tab-content').forEach(content => content.classList.remove('active'));
// Add active class to selected tab
document.querySelector(`[onclick="switchTab('${tabName}')"]`).classList.add('active');
document.getElementById(`${tabName}-tab`).classList.add('active');
if (tabName === 'quiz') {
loadQuizForTaking();
}
}
// Quiz Editor Functions
function addQuestion() {
const container = document.getElementById('questions-container');
const questionCount = container.children.length + 1;
const questionDiv = document.createElement('div');
questionDiv.className = 'question-item';
questionDiv.innerHTML = `
<div class="question-header">
<span class="question-number">Question ${questionCount}</span>
<button class="delete-question" onclick="deleteQuestion(this)">Delete</button>
</div>
<input type="text" class="question-input" placeholder="Enter your question..." value="">
<div class="answers-grid">
<div class="answer-option">
<input type="radio" name="correct-${questionCount}" value="a">
<input type="text" class="answer-input" placeholder="Option A" data-option="a">
</div>
<div class="answer-option">
<input type="radio" name="correct-${questionCount}" value="b">
<input type="text" class="answer-input" placeholder="Option B" data-option="b">
</div>
<div class="answer-option">
<input type="radio" name="correct-${questionCount}" value="c">
<input type="text" class="answer-input" placeholder="Option C" data-option="c">
</div>
<div class="answer-option">
<input type="radio" name="correct-${questionCount}" value="d">
<input type="text" class="answer-input" placeholder="Option D" data-option="d">
</div>
</div>
`;
container.appendChild(questionDiv);
updateQuestionNumbers();
}
function deleteQuestion(button) {
button.closest('.question-item').remove();
updateQuestionNumbers();
}
function updateQuestionNumbers() {
const questions = document.querySelectorAll('.question-item');
questions.forEach((question, index) => {
const questionNumber = question.querySelector('.question-number');
questionNumber.textContent = `Question ${index + 1}`;
// Update radio button names to ensure uniqueness
const radioButtons = question.querySelectorAll('input[type="radio"]');
radioButtons.forEach(radio => {
radio.name = `correct-${index + 1}`;
});
});
}
function saveQuiz() {
const title = document.getElementById('quiz-title').value.trim();
const questionItems = document.querySelectorAll('.question-item');
if (!title) {
showMessage('Please enter a quiz title.', 'error');
return;
}
if (questionItems.length === 0) {
showMessage('Please add at least one question.', 'error');
return;
}
const quizData = [];
let hasError = false;
questionItems.forEach((item, index) => {
const questionText = item.querySelector('.question-input').value.trim();
const answerInputs = item.querySelectorAll('.answer-input');
const correctRadio = item.querySelector('input[type="radio"]:checked');
if (!questionText) {
showMessage(`Question ${index + 1} is missing question text.`, 'error');
hasError = true;
return;
}
if (!correctRadio) {
showMessage(`Question ${index + 1} is missing correct answer selection.`, 'error');
hasError = true;
return;
}
const answers = {};
let emptyAnswers = 0;
answerInputs.forEach(input => {
const option = input.getAttribute('data-option');
const value = input.value.trim();
if (!value) emptyAnswers++;
answers[option] = value;
});
if (emptyAnswers > 0) {
showMessage(`Question ${index + 1} has empty answer options.`, 'error');
hasError = true;
return;
}
quizData.push({
question: questionText,
a: answers.a,
b: answers.b,
c: answers.c,
d: answers.d,
correct: correctRadio.value
});
});
if (hasError) return;
currentQuizData = quizData;
currentQuizTitle = title;
showMessage('Quiz saved successfully!', 'success');
}
function generateShareableLink() {
saveQuiz(); // Save first
if (currentQuizData.length === 0) return;
// Create a shareable data object
const shareData = {
title: currentQuizTitle,
questions: currentQuizData,
mode: 'view'
};
// Encode the data
const encodedData = btoa(JSON.stringify(shareData));
const shareableLink = `${window.location.origin}${window.location.pathname}?quiz=${encodedData}`;
document.getElementById('share-link').value = shareableLink;
document.getElementById('share-section').classList.remove('hidden');
showMessage('Shareable link generated! Others can use this link to take your quiz.', 'success');
}
function copyLink() {
const linkInput = document.getElementById('share-link');
linkInput.select();
linkInput.setSelectionRange(0, 99999);
try {
document.execCommand('copy');
showMessage('Link copied to clipboard!', 'success');
} catch (err) {
console.error('Failed to copy link:', err);
showMessage('Failed to copy link. Please copy manually.', 'error');
}
}
function showMessage(text, type) {
const container = document.getElementById('message-container');
const messageDiv = document.createElement('div');
messageDiv.className = `message ${type}`;
messageDiv.textContent = text;
container.innerHTML = '';
container.appendChild(messageDiv);
setTimeout(() => {
messageDiv.remove();
}, 5000);
}
// Quiz Taking Functions
function loadQuizForTaking() {
currentQuestionIndex = 0;
score = 0;
userAnswers = [];
document.getElementById('quiz-title-display').textContent = currentQuizTitle;
document.getElementById('total-questions').textContent = currentQuizData.length;
document.getElementById('quiz-container').classList.remove('hidden');
document.getElementById('results-container').classList.add('hidden');
displayQuestion();
}
function displayQuestion() {
if (currentQuestionIndex >= currentQuizData.length) {
showResults();
return;
}
const question = currentQuizData[currentQuestionIndex];
document.getElementById('current-question').textContent = currentQuestionIndex + 1;
document.getElementById('question-text').textContent = question.question;
document.getElementById('answer-a').textContent = question.a;
document.getElementById('answer-b').textContent = question.b;
document.getElementById('answer-c').textContent = question.c;
document.getElementById('answer-d').textContent = question.d;
// Clear previous selection
document.querySelectorAll('input[name="answer"]').forEach(radio => radio.checked = false);
// Update button text
const submitButton = document.querySelector('.submit-button');
submitButton.textContent = (currentQuestionIndex === currentQuizData.length - 1) ? 'Finish Quiz' : 'Next Question';
}
function submitAnswer() {
const selectedAnswer = document.querySelector('input[name="answer"]:checked');
if (!selectedAnswer) {
alert('Please select an answer before proceeding.');
return;
}
const userAnswer = selectedAnswer.value;
const correctAnswer = currentQuizData[currentQuestionIndex].correct;
userAnswers.push(userAnswer);
if (userAnswer === correctAnswer) {
score++;
}
currentQuestionIndex++;
displayQuestion();
}
function showResults() {
document.getElementById('quiz-container').classList.add('hidden');
document.getElementById('results-container').classList.remove('hidden');
const percentage = Math.round((score / currentQuizData.length) * 100);
document.getElementById('final-score').textContent = `${score}/${currentQuizData.length}`;
let resultText = '';
if (percentage === 100) {
resultText = 'Perfect! Outstanding performance!';
} else if (percentage >= 80) {
resultText = 'Excellent work! Great job!';
} else if (percentage >= 60) {
resultText = 'Good job! Keep it up!';
} else {
resultText = 'Keep practicing! You can do better!';
}
document.querySelector('.score-text').textContent = resultText;
}
function restartQuiz() {
loadQuizForTaking();
}
// Load quiz from URL parameter on page load
function loadQuizFromURL() {
const urlParams = new URLSearchParams(window.location.search);
const quizParam = urlParams.get('quiz');
if (quizParam) {
try {
const shareData = JSON.parse(atob(quizParam));
if (shareData.mode === 'view' && shareData.questions && shareData.title) {
currentQuizData = shareData.questions;
currentQuizTitle = shareData.title;
// Switch to quiz tab and hide editor
switchTab('quiz');
document.querySelector('[onclick="switchTab(\'editor\')"]').style.display = 'none';
showMessage('Loaded shared quiz! You can now take the quiz.', 'success');
}
} catch (e) {
console.error('Failed to load quiz from URL:', e);
}
}
}
// Initialize the app
function initializeApp() {
// Load default questions in editor
currentQuizData.forEach(() => addQuestion());
const questionItems = document.querySelectorAll('.question-item');
questionItems.forEach((item, index) => {
const data = currentQuizData[index];
item.querySelector('.question-input').value = data.question;
item.querySelector(`[data-option="a"]`).value = data.a;
item.querySelector(`[data-option="b"]`).value = data.b;
item.querySelector(`[data-option="c"]`).value = data.c;
item.querySelector(`[data-option="d"]`).value = data.d;
item.querySelector(`input[value="${data.correct}"]`).checked = true;
});
// Load quiz from URL if present
loadQuizFromURL();
}
// Start the app when page loads
window.addEventListener('load', initializeApp);