-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
248 lines (218 loc) · 8.29 KB
/
Copy pathscript.js
File metadata and controls
248 lines (218 loc) · 8.29 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
const quizData = [
{
question: "What is the output of the following code snippet?",
code: "console.log(typeof null);",
choices: ["object", "null", "undefined", "function"],
correctAnswer: 0
},
{
question: "What is the output of the following code snippet?",
code: "console.log(1 + '1');",
choices: ["11", "2", "undefined", "NaN"],
correctAnswer: 0
},
{
question: "What is the output of the following code snippet?",
code: "console.log(0.1 + 0.2 === 0.3);",
choices: ["true", "false"],
correctAnswer: 1
},
{
question: "What is the output of the following code snippet?",
code: "function foo() {\n console.log(this);\n}\nfoo();",
choices: ["Window object", "undefined", "null", "foo function"],
correctAnswer: 0
},
{
question: "What is the output of the following code snippet?",
code: "console.log(Math.max([2, 4, 6]));",
choices: ["6", "undefined", "NaN", "Error"],
correctAnswer: 2
},
{
question: "What is the output of the following code snippet?",
code: "const person = {\n name: 'John',\n age: 30\n};\nconsole.log(person.name);",
choices: ["'John'", "'name'", "'person'", "undefined"],
correctAnswer: 0
},
{
question: "What is the output of the following code snippet?",
code: "const x = 10;\nfunction foo() {\n console.log(x);\n const x = 20;\n}\nfoo();",
choices: ["10", "20", "undefined", "ReferenceError"],
correctAnswer: 3
},
{
question: "What is the output of the following code snippet?",
code: "console.log(2 + 3 * 4);",
choices: ["14", "20", "10", "23"],
correctAnswer: 0
},
{
question: "What is the output of the following code snippet?",
code: "console.log('2' + 3 * 4);",
choices: ["24", "14", "23", "212"],
correctAnswer: 3
},
{
question: "What is the output of the following code snippet?",
code: "console.log(typeof NaN);",
choices: ["number", "undefined", "NaN", "object"],
correctAnswer: 0
},
{
question: "What is the output of the following code snippet?",
code: "console.log(Array.isArray([]));",
choices: ["true", "false"],
correctAnswer: 0
},
{
question: "What is the output of the following code snippet?",
code: "console.log(1 === '1');",
choices: ["true", "false"],
correctAnswer: 1
}
];
const quizContainer = document.getElementById("quiz-container");
const resultContainer = document.getElementById("result-container");
const questionElement = document.getElementById("question");
const codeElement = document.getElementById("code");
const choicesElement = document.getElementById("choices");
const submitButton = document.getElementById("submit");
const restartButton = document.getElementById("restart");
const resultElement = document.getElementById("result");
const explanationLink = document.getElementById("explanation-link");
const explanationTable = document.getElementById("explanation-table");
let currentQuestion = 0;
let score = 0;
let explanations = [];
function showQuestion() {
const quiz = quizData[currentQuestion];
questionElement.innerText = quiz.question;
codeElement.innerHTML = `<code class="language-javascript">${quiz.code}</code>`;
Prism.highlightAll();
choicesElement.innerHTML = "";
for (let i = 0; i < quiz.choices.length; i++) {
const choice = quiz.choices[i];
const li = document.createElement("li");
li.innerText = choice;
li.dataset.index = i;
li.addEventListener("click", selectAnswer);
choicesElement.appendChild(li);
}
}
function selectAnswer(event) {
const selectedChoice = event.target;
const selectedAnswer = selectedChoice.dataset.index;
const quiz = quizData[currentQuestion];
const correctAnswer = quiz.correctAnswer;
if (selectedAnswer == correctAnswer) {
score++;
explanations.push(true);
} else {
explanations.push(false);
}
// Highlight the selected option
const choices = choicesElement.getElementsByTagName("li");
for (let i = 0; i < choices.length; i++) {
choices[i].classList.remove("selected");
}
selectedChoice.classList.add("selected");
currentQuestion++;
if (currentQuestion < quizData.length) {
showQuestion();
} else {
isAllQuestionsAttempted = true;
submitButton.style.display = "block"; // Display the submit button after all questions are attempted
choicesElement.classList.add("disable-click"); // Disable further selection of options
}
}
// function showResult() {
// quizContainer.style.display = "none";
// resultContainer.style.display = "block";
// resultElement.innerText = `Your score: ${score} out of ${quizData.length}`;
// // Generate explanation table
// explanationTable.innerHTML = "";
// for (let i = 0; i < quizData.length; i++) {
// const quiz = quizData[i];
// const explanationRow = document.createElement("tr");
// explanationRow.innerHTML = `
// <td>${i + 1}</td>
// <td>${quiz.question}</td>
// <td>${quiz.choices[quiz.correctAnswer]}</td>
// <td>${explanations[i] ? "Correct" : "Incorrect"}</td>
// `;
// explanationTable.appendChild(explanationRow);
// }
// // Show the explanation link
// explanationLink.style.display = "block";
// }
// function showResult() {
// quizContainer.style.display = "none";
// resultContainer.style.display = "block";
// resultElement.innerText = `Your score: ${score} out of ${quizData.length}`;
// // Generate explanation table
// const explanationTableBody = document.getElementById("explanation-table");
// explanationTableBody.innerHTML = "";
// for (let i = 0; i < quizData.length; i++) {
// const quiz = quizData[i];
// const explanationRow = document.createElement("tr");
// explanationRow.innerHTML = `
// <td>${i + 1}</td>
// <td>${quiz.question}</td>
// <td>${quiz.choices[quiz.correctAnswer]}</td>
// <td>${explanations[i] ? "Correct" : "Incorrect"}</td>
// `;
// explanationTableBody.appendChild(explanationRow);
// }
// // Show the explanation link
// explanationLink.style.display = "block";
// }
function showResult() {
quizContainer.style.display = "none";
resultContainer.style.display = "block";
resultElement.innerText = `Your score: ${score} out of ${quizData.length}`;
// Generate explanation table
const explanationTableBody = document.getElementById("explanation-table");
explanationTableBody.innerHTML = "";
for (let i = 0; i < quizData.length; i++) {
const quiz = quizData[i];
const explanationRow = document.createElement("tr");
explanationRow.innerHTML = `
<td>${i + 1}</td>
<td>${quiz.question}</td>
<td>${quiz.choices[quiz.correctAnswer]}</td>
<td>${explanations[i] ? "Correct" : "Incorrect"}</td>
`;
explanationTableBody.appendChild(explanationRow);
}
// Construct the URL with the explanations array as a query parameter
const explanationsParam = encodeURIComponent(JSON.stringify(explanations));
const explanationLink = document.getElementById("explanation-link");
explanationLink.href = `explanation.html?explanations=${explanationsParam}`;
// Show the explanation link
explanationLink.style.display = "block";
}
// function restartQuiz() {
// currentQuestion = 0;
// score = 0;
// explanations = [];
// quizContainer.style.display = "block";
// resultContainer.style.display = "none";
// explanationLink.style.display = "none"; // Hide the explanation link when restarting the quiz
// showQuestion();
// }
function restartQuiz() {
currentQuestion = 0;
score = 0;
explanations = [];
quizContainer.style.display = "block";
resultContainer.style.display = "none";
explanationLink.style.display = "none"; // Hide the explanation link when restarting the quiz
submitButton.style.display = "none"; // Hide the submit button when restarting the quiz
choicesElement.classList.remove("disable-click"); // Enable selection of options when restarting the quiz
showQuestion();
}
submitButton.style.display = "none"; // Hide the submit button initially
submitButton.addEventListener("click", showResult);
restartButton.addEventListener("click", restartQuiz);
showQuestion();