-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
322 lines (275 loc) · 12 KB
/
script.js
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
document.addEventListener('DOMContentLoaded', () => {
const questionText = document.getElementById('question-text');
const answerType = document.getElementById('answer-type');
const answerOptions = document.getElementById('answer-options');
const matchQuestions = document.getElementById('match-questions');
const quizContainer = document.getElementById('quiz-container');
const fileInput = document.getElementById('file-input');
let questionCount = 0;
let currentAnswerType = 'radio';
const submitButton = document.getElementById('submit-quiz');
if (submitButton) {
submitButton.addEventListener('click', submitQuiz);
}
const saveButton = document.getElementById('save');
if (saveButton) {
saveButton.addEventListener('click', saveQuestions);
}
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}
function addQuestion() {
if (questionText.value.trim() === '') {
alert('Please enter a question');
return;
}
questionCount++;
const questionHtml = `
<div class="question-container">
<div class="question-header">
<span class="question-number">Question ${questionCount}</span>
</div>
<div class="question-body">
<div class="question-text">${questionText.value}</div>
<div class="question-options">
${currentAnswerType === 'match' ? generateMatchingHTML() : generateOptionsHTML()}
</div>
</div>
</div>
`;
quizContainer.innerHTML += questionHtml;
clearAnswerInputs();
}
function addAnswerInput() {
const answerHtml = `
<div class="answer-option">
<input type="text" placeholder="Answer">
<input type="checkbox"> Mark as correct
</div>
`;
answerOptions.innerHTML += answerHtml;
}
function addMatchQuestion() {
const matchHtml = `
<div class="match-question">
<input type="text" placeholder="Term">
<select class="match-select">
<!-- Options will be added dynamically -->
</select>
</div>
`;
matchQuestions.innerHTML += matchHtml;
}
function generateOptionsHTML() {
let optionsHtml = '';
document.querySelectorAll('.answer-option').forEach((option, index) => {
optionsHtml += `
<label>
<input type="${currentAnswerType}" name="question${questionCount}" value="${index}">
${option.querySelector('input[type="text"]').value}
</label>
`;
});
return optionsHtml;
}
function generateMatchingHTML() {
let matchingHtml = '';
document.querySelectorAll('.match-question').forEach((match) => {
matchingHtml += `
<div class="match-item">
<span class="match-term">${match.querySelector('input[type="text"]').value}</span>
<div class="match-select-container">
<select class="match-select">
<!-- Options will be added dynamically -->
</select>
</div>
</div>
`;
});
return matchingHtml;
}
function clearAnswerInputs() {
answerOptions.innerHTML = '';
for (let i = 0; i < 4; i++) {
addAnswerInput();
}
}
function clearMatchQuestions() {
matchQuestions.innerHTML = '';
}
function handleAnswerTypeChange() {
currentAnswerType = answerType.value;
if (currentAnswerType === 'match') {
clearMatchQuestions();
addMatchQuestion();
} else {
clearAnswerInputs();
}
}
function submitQuiz() {
let score = 0;
const questions = document.querySelectorAll('.question-container');
questions.forEach(question => {
const questionType = question.querySelector('.question-options input')?.type;
let isCorrect = false;
if (questionType === 'radio' || questionType === 'checkbox') {
isCorrect = checkOptions(question, questionType);
} else {
isCorrect = checkMatches(question);
}
if (isCorrect) {
score++;
question.style.backgroundColor = '#ccffcc'; // Green for correct
} else {
question.style.backgroundColor = '#ffcccc'; // Red for incorrect
}
});
alert(`Your score is ${score} out of ${questions.length}`);
}
function checkOptions(question, questionType) {
const options = question.querySelectorAll(`input[type="${questionType}"]`);
var returnVal = true;
for (let option of options) {
const isCorrect = option.getAttribute('iscorrect') === 'true';
if( isCorrect){option.closest('label').setAttribute('style', 'background-color: #a7bc8a;')}else{option.closest('label').setAttribute('style', 'background-color: #ff4554 ;')}
// For radio buttons: if an incorrect option is selected or a correct one is not, return false
if (questionType === 'radio') {
if ((isCorrect && !option.checked) || (!isCorrect && option.checked)) {
// return false;
returnVal = false;
}
}
// For checkboxes: if the checked state doesn't match its correctness, return false
if (questionType === 'checkbox' && ((isCorrect && !option.checked) || (!isCorrect && option.checked))) {
returnVal = false;
}
}
// If all conditions are met, return true
return returnVal;
}
function checkMatches(question) {
const matchItems = question.querySelectorAll('.match-item');
for (let matchItem of matchItems) {
const term = matchItem.querySelector('.match-term').textContent.trim();
const selectedValue = matchItem.querySelector('.match-select').value;
// Compare the term with the selected value
if (term !== selectedValue) {
return false;
}
}
return true;
}
function saveQuestions() {
console.log('saveQuestions');
const questions = document.querySelectorAll('.question-container');
const quizData = [];
questions.forEach(question => {
const questionText = question.querySelector('.question-text').textContent;
const questionType = question.querySelector('.question-options input').type;
const answers = [];
if (questionType === 'radio' || questionType === 'checkbox') {
const options = question.querySelectorAll('.question-options input');
options.forEach((option, index) => {
answers.push({
answerValue: option.nextSibling.textContent.trim(),
isCorrect: option.nextElementSibling.textContent.trim() === 'Mark as correct'
});
});
} else if (questionType === 'select-one') {
const matches = question.querySelectorAll('.match-item');
matches.forEach(match => {
answers.push({
term: match.querySelector('.match-term').textContent.trim(),
correctValue: match.querySelector('.match-select').value
});
});
}
quizData.push({
questionText: questionText,
answers: answers,
type: questionType === 'select-one' ? 'match' : questionType
});
});
const dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(quizData, null, 2));
const downloadAnchorNode = document.createElement('a');
downloadAnchorNode.setAttribute("href", dataStr);
downloadAnchorNode.setAttribute("download", "quiz_data.json");
document.body.appendChild(downloadAnchorNode); // Required for Firefox
downloadAnchorNode.click();
downloadAnchorNode.remove();
}
function loadQuestions(event) {
const file = event.target.files[0];
if (!file) {
return;
}
const reader = new FileReader();
reader.onload = function(e) {
const content = e.target.result;
let questions;
try {
questions = JSON.parse(content);
if (!Array.isArray(questions)) {
throw new Error('Invalid format');
}
} catch (err) {
alert('Error reading file: ' + err.message);
return;
}
loadQuestionsToDOM(questions);
};
reader.readAsText(file);
}
function loadQuestionsToDOM(questions) {
shuffleArray(questions);
const quizContainer = document.getElementById('quiz-container');
quizContainer.innerHTML = ''; // Clear existing questions
questions.forEach((question, index) => {
let optionsHTML = '';
const formattedQuestionText = question.questionText.replace(/\n/g, '<br>');
if (question.type === 'radio' || question.type === 'checkbox') {
shuffleArray(question.answers);
question.answers.forEach((answer, answerIndex) => {
optionsHTML += `
<label>
<input type="${question.type}" name="question${index}" value="${answerIndex}", isCorrect="${answer.isCorrect}">
${answer.answerValue}
</label>
`;
});
} else if (question.type === 'match') {
question.answers.forEach((answer) => {
optionsHTML += `
<div class="match-item">
<span class="match-term">${answer.term}</span>
<div class="match-select-container">
<select class="match-select">
${question.answers.map(a => `<option value="${a.term}">${a.correctValue}</option>`).join('')}
</select>
</div>
</div>
`;
});
}
const questionHtml = `
<div class="question-container">
<div class="question-header">
<span class="question-number">Question ${index + 1}</span>
</div>
<div class="question-body">
<div class="question-text">${formattedQuestionText}</div>
<div class="question-options">${optionsHTML}</div>
</div>
</div>
`;
quizContainer.innerHTML += questionHtml;
});
}
answerType.addEventListener('change', handleAnswerTypeChange);
fileInput.addEventListener('change', loadQuestions);
// Initialize the application
handleAnswerTypeChange();
});