-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
356 lines (327 loc) · 10.6 KB
/
Copy pathscript.js
File metadata and controls
356 lines (327 loc) · 10.6 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
const questions = [
// Easy
{
question: "What does CPU stand for?",
options: ["Central Process Unit", "Central Processing Unit", "Computer Personal Unit", "Central Processor Utility"],
answer: "Central Processing Unit"
},
{
question: "Which language is primarily used for web development?",
options: ["Python", "C++", "JavaScript", "Java"],
answer: "JavaScript"
},
{
question: "What does RAM stand for?",
options: ["Read Access Memory", "Random Access Memory", "Run Access Memory", "Random Allocate Memory"],
answer: "Random Access Memory"
},
{
question: "Which of these is NOT an operating system?",
options: ["Windows", "Linux", "Oracle", "macOS"],
answer: "Oracle"
},
{
question: "Which symbol is used for comments in JavaScript?",
options: ["//", "/* */", "#", "<!-- -->"],
answer: "//"
},
{
question: "What is the value of binary 1010?",
options: ["10", "12", "8", "14"],
answer: "10"
},
{
question: "Which one is a frontend framework?",
options: ["Django", "React", "Node.js", "Flask"],
answer: "React"
},
{
question: "Which one is a version control system?",
options: ["MySQL", "Git", "Docker", "Linux"],
answer: "Git"
},
{
question: "HTML is used to:",
options: ["Add style", "Store data", "Structure web pages", "Create database"],
answer: "Structure web pages"
},
{
question: "Which is a database management system?",
options: ["Photoshop", "Figma", "MySQL", "Node.js"],
answer: "MySQL"
},
{
question: "Which keyword is used to define a variable in JavaScript?",
options: ["var", "let", "const", "All of the above"],
answer: "All of the above"
},
{
question: "Which of these is not a data type in JavaScript?",
options: ["String", "Boolean", "Float", "Number"],
answer: "Float"
},
{
question: "What does the 'if' statement do?",
options: ["Loops over code", "Executes code conditionally", "Declares a variable", "Ends a loop"],
answer: "Executes code conditionally"
},
{
question: "What is the correct way to write an array in JavaScript?",
options: ["var arr = {}", "var arr = []", "var arr = ()", "var arr = <>"],
answer: "var arr = []"
},
{
question: "CSS is used to:",
options: ["Add logic", "Create structure", "Style web pages", "Connect databases"],
answer: "Style web pages"
},
{
question: "Which of the following is a loop in JavaScript?",
options: ["repeat", "iterate", "for", "loop"],
answer: "for"
},
{
question: "Which protocol is used to browse websites?",
options: ["FTP", "HTTP", "TCP", "SSH"],
answer: "HTTP"
},
{
question: "Which device is used to store data permanently?",
options: ["RAM", "ROM", "Hard Drive", "Cache"],
answer: "Hard Drive"
},
{
question: "Which of these is a markup language?",
options: ["CSS", "HTML", "Python", "Java"],
answer: "HTML"
},
{
question: "Who is known as the father of the computer?",
options: ["Bill Gates", "Alan Turing", "Charles Babbage", "Steve Jobs"],
answer: "Charles Babbage"
},
//Medium Difficulty
{
question: "What is the output of 2 + '2' in JavaScript?",
options: ["4", "22", "NaN", "Error"],
answer: "22"
},
{
question: "What does API stand for?",
options: ["Application Programming Interface", "Applied Protocol Interface", "Advanced Program Interaction", "Application Port Integration"],
answer: "Application Programming Interface"
},
{
question: "Which HTML tag is used to include JavaScript code?",
options: ["<script>", "<js>", "<code>", "<javascript>"],
answer: "<script>"
},
{
question: "Which method is used to fetch data from an API in JavaScript?",
options: ["getData()", "fetch()", "receive()", "httpRequest()"],
answer: "fetch()"
},
{
question: "In CSS, what does 'em' unit measure?",
options: ["Pixels", "Percentage", "Relative to parent font-size", "Relative to viewport"],
answer: "Relative to parent font-size"
},
{
question: "What is the output of `typeof null` in JavaScript?",
options: ["'null'", "'object'", "'undefined'", "'boolean'"],
answer: "'object'"
},
{
question: "Which of the following is a NoSQL database?",
options: ["PostgreSQL", "MongoDB", "MySQL", "Oracle"],
answer: "MongoDB"
},
{
question: "Which HTTP method is used to update data?",
options: ["GET", "PUT", "POST", "DELETE"],
answer: "PUT"
},
{
question: "What does DNS stand for?",
options: ["Data Network System", "Domain Name System", "Digital Node Server", "Domain Network Source"],
answer: "Domain Name System"
},
{
question: "What does the '===' operator check in JavaScript?",
options: ["Value only", "Type only", "Both value and type", "None of the above"],
answer: "Both value and type"
},
// Difficult Difficulty
{
question: "What is a closure in JavaScript?",
options: [
"A function having access to the parent scope",
"A function that executes immediately",
"A function that runs on page load",
"A loop that never ends"
],
answer: "A function having access to the parent scope"
},
{
question: "Which sorting algorithm has the best average-case time complexity?",
options: ["Bubble Sort", "Merge Sort", "Selection Sort", "Insertion Sort"],
answer: "Merge Sort"
},
{
question: "What is Big O notation used for?",
options: ["Styling elements", "Measuring space", "Describing performance or complexity", "Describing syntax"],
answer: "Describing performance or complexity"
},
{
question: "Which of these is a strongly typed language?",
options: ["Python", "Java", "JavaScript", "PHP"],
answer: "Java"
},
{
question: "What is the space complexity of a binary search algorithm?",
options: ["O(1)", "O(n)", "O(log n)", "O(n log n)"],
answer: "O(1)"
},
{
question: "Which protocol secures data over the internet?",
options: ["FTP", "HTTP", "SSL/TLS", "IP"],
answer: "SSL/TLS"
},
{
question: "Which data structure uses LIFO?",
options: ["Queue", "Linked List", "Stack", "Tree"],
answer: "Stack"
},
{
question: "What is a race condition in programming?",
options: [
"A bug that causes an infinite loop",
"An error from slow code",
"An unexpected outcome due to timing in concurrent operations",
"A problem with version control"
],
answer: "An unexpected outcome due to timing in concurrent operations"
},
{
question: "Which layer of the OSI model handles encryption?",
options: ["Application", "Presentation", "Transport", "Session"],
answer: "Presentation"
},
{
question: "What is the function of a virtual machine (VM)?",
options: [
"To host websites",
"To monitor network traffic",
"To emulate a computer system",
"To store user passwords"
],
answer: "To emulate a computer system"
}
];
const questionText = document.querySelector(".question");
const optionsList = document.querySelector(".multichoice ul");
const nextBtn = document.querySelector(".ques-btn");
const questionCounter = document.querySelector("h2");
const timerBar = document.querySelector(".timer-bar");
const timeLeft = document.getElementById("time-left");
const timerContainer = document.querySelector(".timer-container");
let currentQuestion = 0;
let score = 0;
let countdownInterval;
let remainingTime = 10;
const randomQuestions = shuffleArray([...questions]).slice(0, 10)
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]];
}
return array;
}
function loadQuestion() {
const q = randomQuestions[currentQuestion];
questionText.textContent = q.question;
questionCounter.textContent = `Question ${currentQuestion + 1}/10`;
optionsList.innerHTML = "";
q.options.forEach(option => {
const li = document.createElement("li");
li.textContent = option;
li.onclick = () => selectOption(li, q.answer);
optionsList.appendChild(li);
});
startTimer();
}
function startTimer() {
remainingTime = 10;
timeLeft.textContent = `${remainingTime}s`;
timerBar.style.width = "100%";
countdownInterval = setInterval(() => {
remainingTime--;
timeLeft.textContent = `${remainingTime}s`;
timerBar.style.width = `${(remainingTime / 10) * 100}%`;
if (remainingTime < 0) {
clearInterval(countdownInterval);
handleTimeout();
}
}, 1000)
}
function selectOption(selectedLi, correctAnswer) {
const allOptions = optionsList.querySelectorAll("li");
allOptions.forEach(opt => {
opt.style.pointerEvents = "none";
});
if (selectedLi.textContent === correctAnswer) {
selectedLi.style.backgroundColor = "var(--correct)";
score++;
} else {
selectedLi.style.backgroundColor = "var(--wrong)";
}
allOptions.forEach(opt => {
if (opt.textContent === correctAnswer) {
opt.style.backgroundColor = "var(--correct)"
}
});
}
function handleTimeout() {
currentQuestion++;
if (currentQuestion < 10) {
loadQuestion();
} else {
showFinalScore();
}
}
function showFinalScore() {
const app = document.querySelector(".app");
app.innerHTML = `<h2>Quiz Completed!</h2>
<p>Your Score: ${score}/10</p>`;
if (score === 10) {
confetti({
particleCount: 200,
spread: 70,
origin: {y: 0.6}
});
}
const restartBtn = document.createElement("button");
restartBtn.classList.add("btn");
restartBtn.textContent = "Try Again";
restartBtn.addEventListener("click", () => {
location.reload();
});
const exitBtn = document.createElement("button");
exitBtn.classList.add("btn");
exitBtn.textContent = "Exit";
exitBtn.addEventListener("click", () =>{
window.location.href = "index.html";
});
app.appendChild(restartBtn);
app.appendChild(exitBtn);
}
nextBtn.addEventListener("click", () => {
clearInterval(countdownInterval);
currentQuestion++;
if (currentQuestion < 10) {
loadQuestion();
} else {
showFinalScore()
}
});
loadQuestion();