-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
258 lines (224 loc) · 12.3 KB
/
script.js
File metadata and controls
258 lines (224 loc) · 12.3 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
let timer;
let totalSeconds = 10800; // 3 hours in seconds
let quizData = {
mcqs: [
{ image: "questions/physics_MCQ/1.png", options: ["A", "B", "C", "D"], answer: "A" },
{ image: "questions/physics_MCQ/2.png", options: ["A", "B", "C", "D"], answer: "D" },
{ image: "questions/physics_MCQ/3.png", options: ["A", "B", "C", "D"], answer: "C" },
{ image: "questions/physics_MCQ/4.png", options: ["A", "B", "C", "D"], answer: "D" },
{ image: "questions/physics_MCQ/5.png", options: ["A", "B", "C", "D"], answer: "A" },
{ image: "questions/physics_MCQ/6.png", options: ["A", "B", "C", "D"], answer: "D" },
{ image: "questions/physics_MCQ/7.png", options: ["A", "B", "C", "D"], answer: "B" },
{ image: "questions/physics_MCQ/8.png", options: ["A", "B", "C", "D"], answer: "A" },
{ image: "questions/physics_MCQ/9.png", options: ["A", "B", "C", "D"], answer: "A" },
{ image: "questions/physics_MCQ/10.png", options: ["A", "B", "C", "D"], answer: "C" },
{ image: "questions/physics_MCQ/11.png", options: ["A", "B", "C", "D"], answer: "B" },
{ image: "questions/physics_MCQ/12.png", options: ["A", "B", "C", "D"], answer: "D" },
{ image: "questions/physics_MCQ/13.png", options: ["A", "B", "C", "D"], answer: "D" },
{ image: "questions/physics_MCQ/14.png", options: ["A", "B", "C", "D"], answer: "B" },
{ image: "questions/physics_MCQ/15.png", options: ["A", "B", "C", "D"], answer: "B" },
{ image: "questions/physics_MCQ/16.png", options: ["A", "B", "C", "D"], answer: "B" },
{ image: "questions/physics_MCQ/17.png", options: ["A", "B", "C", "D"], answer: "A" },
{ image: "questions/physics_MCQ/18.png", options: ["A", "B", "C", "D"], answer: "D" },
{ image: "questions/physics_MCQ/19.png", options: ["A", "B", "C", "D"], answer: "A" },
{ image: "questions/physics_MCQ/20.png", options: ["A", "B", "C", "D"], answer: "C" },
{ image: "questions/chem_mcq/1.png", options: ["A", "B", "C", "D"], answer: "D" },
{ image: "questions/chem_mcq/2.png", options: ["A", "B", "C", "D"], answer: "A" },
{ image: "questions/chem_mcq/3.png", options: ["A", "B", "C", "D"], answer: "A" },
{ image: "questions/chem_mcq/4.png", options: ["A", "B", "C", "D"], answer: "B" },
{ image: "questions/chem_mcq/5.png", options: ["A", "B", "C", "D"], answer: "B" },
{ image: "questions/chem_mcq/6.png", options: ["A", "B", "C", "D"], answer: "D" },
{ image: "questions/chem_mcq/7.png", options: ["A", "B", "C", "D"], answer: "A" },
{ image: "questions/chem_mcq/8.png", options: ["A", "B", "C", "D"], answer: "A" },
{ image: "questions/chem_mcq/9.png", options: ["A", "B", "C", "D"], answer: "A" },
{ image: "questions/chem_mcq/10.png", options: ["A", "B", "C", "D"], answer: "A" },
{ image: "questions/chem_mcq/11.png", options: ["A", "B", "C", "D"], answer: "D" },
{ image: "questions/chem_mcq/12.png", options: ["A", "B", "C", "D"], answer: "C" },
{ image: "questions/chem_mcq/13.png", options: ["A", "B", "C", "D"], answer: "B" },
{ image: "questions/chem_mcq/14.png", options: ["A", "B", "C", "D"], answer: "C" },
{ image: "questions/chem_mcq/15.png", options: ["A", "B", "C", "D"], answer: "C" },
{ image: "questions/chem_mcq/16.png", options: ["A", "B", "C", "D"], answer: "B" },
{ image: "questions/chem_mcq/17.png", options: ["A", "B", "C", "D"], answer: "C" },
{ image: "questions/chem_mcq/18.png", options: ["A", "B", "C", "D"], answer: "C" },
{ image: "questions/chem_mcq/19.png", options: ["A", "B", "C", "D"], answer: "D" },
{ image: "questions/chem_mcq/20.png", options: ["A", "B", "C", "D"], answer: "C" },
{ image: "questions/maths_mcq/1.png", options: ["A", "B", "C", "D"], answer: "B" },
{ image: "questions/maths_mcq/2.png", options: ["A", "B", "C", "D"], answer: "A" },
{ image: "questions/maths_mcq/3.png", options: ["A", "B", "C", "D"], answer: "A" },
{ image: "questions/maths_mcq/4.png", options: ["A", "B", "C", "D"], answer: "A" },
{ image: "questions/maths_mcq/5.png", options: ["A", "B", "C", "D"], answer: "B" },
{ image: "questions/maths_mcq/6.png", options: ["A", "B", "C", "D"], answer: "D" },
{ image: "questions/maths_mcq/7.png", options: ["A", "B", "C", "D"], answer: "B" },
{ image: "questions/maths_mcq/8.png", options: ["A", "B", "C", "D"], answer: "D" },
{ image: "questions/maths_mcq/9.png", options: ["A", "B", "C", "D"], answer: "B" },
{ image: "questions/maths_mcq/10.png", options: ["A", "B", "C", "D"], answer: "A" },
{ image: "questions/maths_mcq/11.png", options: ["A", "B", "C", "D"], answer: "C" },
{ image: "questions/maths_mcq/12.png", options: ["A", "B", "C", "D"], answer: "B" },
{ image: "questions/maths_mcq/13.png", options: ["A", "B", "C", "D"], answer: "C" },
{ image: "questions/maths_mcq/14.png", options: ["A", "B", "C", "D"], answer: "B" },
{ image: "questions/maths_mcq/15.png", options: ["A", "B", "C", "D"], answer: "D" },
{ image: "questions/maths_mcq/16.png", options: ["A", "B", "C", "D"], answer: "C" },
{ image: "questions/maths_mcq/17.png", options: ["A", "B", "C", "D"], answer: "B" },
{ image: "questions/maths_mcq/18.png", options: ["A", "B", "C", "D"], answer: "A" },
{ image: "questions/maths_mcq/19.png", options: ["A", "B", "C", "D"], answer: "A" },
{ image: "questions/maths_mcq/20.png", options: ["A", "B", "C", "D"], answer: "C" }
],
subjective: [
{ image: "questions/physics_sub/1.png", answer: "38" },
{ image: "questions/physics_sub/2.png", answer: "3" },
{ image: "questions/physics_sub/3.png", answer: "15" },
{ image: "questions/physics_sub/4.png", answer: "2" },
{ image: "questions/physics_sub/5.png", answer: "2" },
{ image: "questions/chem_sub/1.png", answer: "0" },
{ image: "questions/chem_sub/2.png", answer: "8" },
{ image: "questions/chem_sub/3.png", answer: "28" },
{ image: "questions/chem_sub/4.png", answer: "3" },
{ image: "questions/chem_sub/5.png", answer: "5" },
{ image: "questions/maths_sub/1.png", answer: "12" },
{ image: "questions/maths_sub/2.png", answer: "60" },
{ image: "questions/maths_sub/3.png", answer: "461" },
{ image: "questions/maths_sub/4.png", answer: "2" },
{ image: "questions/maths_sub/5.png", answer: "4" }
]
};
let responses = {
mcqs: new Array(quizData.mcqs.length).fill(null),
subjective: new Array(quizData.subjective.length).fill(null)
};
let currentQuestionIndex = 0;
window.onload = function() {
startTimer();
displayQuestion();
};
function startTimer() {
timer = setInterval(function() {
if (totalSeconds <= 0) {
submitQuiz();
} else {
totalSeconds--;
document.getElementById("timer").innerText = formatTime(totalSeconds);
}
}, 1000);
}
function formatTime(seconds) {
let hrs = Math.floor(seconds / 3600);
let mins = Math.floor((seconds % 3600) / 60);
let secs = seconds % 60;
return `${hrs.toString().padStart(2, '0')}:${mins.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;
}
function displayQuestion() {
const questionIndex = currentQuestionIndex;
const question = quizData.mcqs[questionIndex] || quizData.subjective[questionIndex - quizData.mcqs.length];
const questionImage = document.getElementById("questionImage");
const optionsContainer = document.getElementById("options");
// Clear previous content
questionImage.innerHTML = '';
optionsContainer.innerHTML = '';
if (question) {
// Display question image
if (question.image) {
questionImage.innerHTML = `<img src="${question.image}" alt="Question Image">`;
}
// Display options
if (question.options) {
question.options.forEach(option => {
const checked = responses.mcqs[questionIndex] === option ? 'checked' : '';
optionsContainer.innerHTML += `
<label class="option">
<input type="radio" name="mcq${questionIndex}" value="${option}" ${checked} onclick="saveResponse('mcq', ${questionIndex}, this.value)"> ${option}
</label>
`;
});
} else {
// Subjective question (textarea)
const response = responses.subjective[questionIndex - quizData.mcqs.length] || '';
optionsContainer.innerHTML = `
<textarea rows="4" cols="50" oninput="saveResponse('subjective', ${questionIndex - quizData.mcqs.length}, this.value)">${response}</textarea>
`;
}
}
// Navigation button visibility
document.getElementById("prevButton").style.display = currentQuestionIndex === 0 ? 'none' : 'inline-block';
document.getElementById("nextButton").style.display = currentQuestionIndex === (quizData.mcqs.length + quizData.subjective.length - 1) ? 'none' : 'inline-block';
}
function saveResponse(type, index, value) {
if (type === 'mcq') {
responses.mcqs[index] = value;
} else if (type === 'subjective') {
responses.subjective[index] = value;
}
}
function nextQuestion() {
if (currentQuestionIndex < (quizData.mcqs.length + quizData.subjective.length - 1)) {
currentQuestionIndex++;
displayQuestion();
}
}
function prevQuestion() {
if (currentQuestionIndex > 0) {
currentQuestionIndex--;
displayQuestion();
}
}
function submitQuiz() {
clearInterval(timer);
const totalAttempted = responses.mcqs.filter(response => response !== null).length +
Math.min(responses.subjective.filter(response => response !== null && response.trim() !== "").length, 15);
let totalMarks = 0;
// Calculate MCQ results
responses.mcqs.forEach((response, index) => {
if (response !== null) {
totalMarks += response === quizData.mcqs[index].answer ? 4 : -1;
}
});
// Calculate Subjective results
let subjectiveAttempted = 0;
responses.subjective.forEach((response, index) => {
if (response !== null && response.trim() !== "") {
subjectiveAttempted++;
if (subjectiveAttempted <= 15) {
totalMarks += response.trim().toLowerCase() === quizData.subjective[index].answer.toLowerCase() ? 4 : -1;
}
}
});
// Confirm submission
const confirmationMessage = `You have attempted ${totalAttempted} questions. Are you sure you want to submit the quiz?`;
if (window.confirm(confirmationMessage)) {
// Display results
document.getElementById("quizForm").style.display = "none";
document.getElementById("result").style.display = "block";
document.getElementById("totalAttempted").innerText = `Total Attempted: ${totalAttempted}`;
document.getElementById("totalMarks").innerText = `Total Marks: ${totalMarks}`;
const username = localStorage.getItem("quizUsername");
downloadResponses();
} else {
startTimer();
}
function downloadResponses() {
// Create CSV content
let csvContent = "data:text/csv;charset=utf-8,";
// Add headers
csvContent += "Type,Question No.,Your Response,Correct Answer\n";
// Add MCQ responses
responses.mcqs.forEach((response, index) => {
const correctAnswer = quizData.mcqs[index].answer;
csvContent += `MCQ,${index + 1},${response || ''},${correctAnswer}\n`;
});
// Add Subjective responses
responses.subjective.forEach((response, index) => {
const correctAnswer = quizData.subjective[index].answer;
csvContent += `Subjective,${index + 1},${response || ''},${correctAnswer}\n`;
});
// Encode CSV content
const encodedUri = encodeURI(csvContent);
// Create a temporary link element
const link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", "response_with_answers.csv");
document.body.appendChild(link);
// Trigger the download
link.click();
// Clean up
document.body.removeChild(link);
}
}