-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
221 lines (194 loc) · 7.04 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
let questions = [];
let currentQuestion = 0;
let userAnswers = {};
let flaggedQuestions = [];
let quizGraded = false;
let quizTitle = '';
// Adjusted function for handling dropdown selection
function loadSelectedJsonFile() {
const dropdown = document.getElementById("quizDropdown");
const selectedFile = dropdown.value;
if (!selectedFile) return;
flaggedQuestions = [];
quizGraded = false;
quizTitle = dropdown.options[dropdown.selectedIndex].text; // Get quiz title from dropdown
document.getElementById("title").innerText = quizTitle; // Update quiz title in the <h1>
loadQuestions(selectedFile);
}
// Load and parse index.json, then populate dropdown
function loadIndex() {
fetch("index.json")
.then(response => response.json())
.then(data => {
const files = data.files;
const dropdown = document.getElementById("quizDropdown");
files.forEach(file => {
const option = document.createElement("option");
option.value = file.file;
option.innerText = file.title;
dropdown.appendChild(option);
});
})
.catch(error => {
console.error("Error loading index.json:", error);
});
}
function toggleFlag() {
const questionIndex = currentQuestion;
if (flaggedQuestions.includes(questionIndex)) {
flaggedQuestions = flaggedQuestions.filter(q => q !== questionIndex);
} else {
flaggedQuestions.push(questionIndex);
}
setCookie("flaggedQuestions", JSON.stringify(flaggedQuestions), 7);
renderQuestionList();
}
function loadQuestions(file) {
console.log (file);
fetch(file)
.then(response => response.json())
.then(data => {
questions = data;
shuffleArray(questions);
questions.forEach(question => {
shuffleArray(question.options);
});
renderQuestionList();
displayQuestion();
document.getElementById("scoreDisplay").innerText = `Score: Not graded yet`;
})
.catch(error => {
console.error("Error loading questions file:", error);
});
}
/*function renderQuestionList() {
const questionList = document.getElementById("questionList");
questionList.innerHTML = "";
questions.forEach((question, index) => {
const listItem = document.createElement("li");
listItem.innerText = `Question ${index + 1}`;
if (flaggedQuestions.includes(index)) {
listItem.innerText += " 🚩"; // Flag indicator
}
if (currentQuestion === index) {
listItem.style.fontWeight = "bold";
}
listItem.onclick = () => {
currentQuestion = index;
displayQuestion();
renderQuestionList();
};
questionList.appendChild(listItem);
});
}*/
function renderQuestionList() {
const questionList = document.getElementById("questionList");
questionList.innerHTML = "";
questions.forEach((question, index) => {
const listItem = document.createElement("li");
listItem.innerText = `Question ${index + 1}`;
if (flaggedQuestions.includes(index)) {
listItem.innerText += " 🚩"; // Add flag emoji if flagged
}
listItem.className = flaggedQuestions.includes(index) ? "flagged" : "";
if (currentQuestion == index) {
listItem.style.fontWeight = "bold";
}
if (quizGraded) {
console.log(quizGraded);
const isCorrect = userAnswers[index] === question.answer;
listItem.style.color = isCorrect ? "green" : "red";
listItem.innerText += isCorrect ? " ✅" : " ❌";
}
listItem.onclick = () => {
currentQuestion = index;
displayQuestion();
renderQuestionList();
};
questionList.appendChild(listItem);
});
}
function displayQuestion() {
const questionData = questions[currentQuestion];
const questionElement = document.getElementById("question");
const optionsForm = document.getElementById("optionsForm");
questionElement.innerText = questionData.question;
optionsForm.innerHTML = ""; // Clear previous options
questionData.options.forEach(option => {
const optionContainer = document.createElement("div");
const optionInput = document.createElement("input");
optionInput.type = "radio";
optionInput.name = "option";
optionInput.value = option;
optionInput.checked = userAnswers[currentQuestion] === option;
optionInput.onclick = () => selectOption(option);
const optionLabel = document.createElement("label");
optionLabel.innerText = option;
optionContainer.appendChild(optionInput);
optionContainer.appendChild(optionLabel);
optionsForm.appendChild(optionContainer);
if (quizGraded && option === questions[currentQuestion].answer) {
optionLabel.style.color = "green";
optionLabel.style.fontWeight = "bold"; // Optional: Bold the correct answer
}
});
}
function selectOption(selected) {
userAnswers[currentQuestion] = selected;
}
function nextQuestion() {
if (currentQuestion < questions.length - 1) {
currentQuestion++;
displayQuestion();
renderQuestionList();
}
}
function prevQuestion() {
if (currentQuestion > 0) {
currentQuestion--;
displayQuestion();
renderQuestionList();
}
}
/*function gradeQuiz() {
let score = 0;
questions.forEach((question, index) => {
const isCorrect = userAnswers[index] === question.answer;
if (isCorrect) score++;
});
const percentage = ((score / questions.length) * 100).toFixed(2);
document.getElementById("scoreDisplay").innerText = `Score: ${score} / ${questions.length} (${percentage}%)`;
quizGraded = true;
renderQuestionList();
}
*/
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 setCookie(name, value, days) {
const date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
const expires = `expires=${date.toUTCString()}`;
document.cookie = `${name}=${value};${expires};path=/`;
}
function gradeQuiz() {
let score = 0;
questions.forEach((question, index) => {
const isCorrect = userAnswers[index] === question.answer;
if (isCorrect) score++;
});
// Calculate the percentage
const percentage = ((score / questions.length) * 100).toFixed(2); // rounded to 2 decimal places
// Update the score display with raw score and percentage
document.getElementById("scoreDisplay").innerText = `Score: ${score} / ${questions.length} (${percentage}%)`;
quizGraded = true;
renderQuestionList();
displayQuestion();
}
// Load index.json and populate the dropdown when the page is ready
document.addEventListener("DOMContentLoaded", () => {
loadIndex();
});