-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
493 lines (431 loc) · 17.9 KB
/
script.js
File metadata and controls
493 lines (431 loc) · 17.9 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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
// Add active class to current page in sidebar
document.addEventListener('DOMContentLoaded', function() {
const currentPage = window.location.pathname.split('/').pop();
const sidebarLinks = document.querySelectorAll('.sidebar a');
sidebarLinks.forEach(link => {
if (link.getAttribute('href') === currentPage) {
link.classList.add('active');
}
});
// Mobile menu toggle
const menuButton = document.createElement('button');
menuButton.className = 'md:hidden fixed top-4 right-4 bg-gray-800 text-white p-2 rounded';
menuButton.innerHTML = '<i class="fas fa-bars"></i>';
document.body.appendChild(menuButton);
const sidebar = document.querySelector('.sidebar');
menuButton.addEventListener('click', () => {
sidebar.classList.toggle('hidden');
});
// Code copy functionality
document.querySelectorAll('pre').forEach((pre) => {
const copyButton = document.createElement('button');
copyButton.className = 'absolute top-2 right-2 bg-gray-700 text-white px-2 py-1 rounded text-sm';
copyButton.textContent = 'Copy';
pre.style.position = 'relative';
pre.appendChild(copyButton);
copyButton.addEventListener('click', () => {
const code = pre.querySelector('code').textContent;
navigator.clipboard.writeText(code).then(() => {
copyButton.textContent = 'Copied!';
setTimeout(() => {
copyButton.textContent = 'Copy';
}, 2000);
});
});
});
});
// Quiz answer checking function for day2.html
function checkAnswers() {
const currentPage = window.location.pathname.split('/').pop();
if (currentPage === 'day2.html') {
const answers = {
q1: 'A', // GET is idempotent
q2: 'B', // 201 Created
q3: 'False', // GET requests typically don't have a body
q4: 'False', // POST requests are not safe methods
q5a: 'sunt aut facere repellat provident occaecati excepturi optio reprehenderit', // Title of post 1
q5b: '201' // Status code for successful POST
};
let score = 0;
const totalQuestions = 5;
// Check multiple choice and true/false
for (let i = 1; i <= 4; i++) {
const selected = document.querySelector(`input[name="q${i}"]:checked`);
if (selected) {
const answer = selected.parentElement.textContent.trim();
if (answer.startsWith(answers[`q${i}`])) {
score++;
selected.parentElement.classList.add('text-green-600');
} else {
selected.parentElement.classList.add('text-red-600');
}
}
}
// Check practical exercise
const postTitle = document.querySelector('input[type="text"]').value;
const statusCode = document.querySelector('input[type="number"]').value;
if (postTitle === answers.q5a) {
score++;
document.querySelector('input[type="text"]').classList.add('border-green-500');
} else {
document.querySelector('input[type="text"]').classList.add('border-red-500');
}
if (statusCode === answers.q5b) {
score++;
document.querySelector('input[type="number"]').classList.add('border-green-500');
} else {
document.querySelector('input[type="number"]').classList.add('border-red-500');
}
// Show result
alert(`You scored ${score} out of ${totalQuestions}!`);
}
}
// Quiz answer checking function for day3.html
function checkAnswers() {
const currentPage = window.location.pathname.split('/').pop();
if (currentPage === 'day3.html') {
const answers = {
q1: 'B', // 201 Created
q2: 'B', // Too Many Requests
q3: 'False', // 500 is a server-side error
q4: 'True', // Both are redirects
q5a: '404', // Not Found
q5b: '200', // OK
q5c: '400' // Bad Request
};
let score = 0;
const totalQuestions = 5;
// Check multiple choice and true/false
for (let i = 1; i <= 4; i++) {
const selected = document.querySelector(`input[name="q${i}"]:checked`);
if (selected) {
const answer = selected.parentElement.textContent.trim();
if (answer.startsWith(answers[`q${i}`])) {
score++;
selected.parentElement.classList.add('text-green-600');
} else {
selected.parentElement.classList.add('text-red-600');
}
}
}
// Check practical exercise
const statusCodes = document.querySelectorAll('input[type="number"]');
const expectedCodes = ['q5a', 'q5b', 'q5c'];
statusCodes.forEach((input, index) => {
if (input.value === answers[expectedCodes[index]]) {
score++;
input.classList.add('border-green-500');
} else {
input.classList.add('border-red-500');
}
});
// Show result
alert(`You scored ${score} out of ${totalQuestions}!`);
}
}
// Quiz answer checking function for day4.html
function checkAnswers() {
const currentPage = window.location.pathname.split('/').pop();
if (currentPage === 'day4.html') {
const answers = {
q1: 'B', // PUT is used to completely replace a resource
q2: 'B', // 204 No Content
q3: 'True', // PUT requests are idempotent
q4: 'False', // DELETE requests typically don't have a body
q5a: '200', // PUT request status code
q5b: '200' // DELETE request status code
};
let score = 0;
const totalQuestions = 5;
// Check multiple choice and true/false
for (let i = 1; i <= 4; i++) {
const selected = document.querySelector(`input[name="q${i}"]:checked`);
if (selected) {
const answer = selected.parentElement.textContent.trim();
if (answer.startsWith(answers[`q${i}`])) {
score++;
selected.parentElement.classList.add('text-green-600');
} else {
selected.parentElement.classList.add('text-red-600');
}
}
}
// Check practical exercise
const statusCodes = document.querySelectorAll('input[type="number"]');
const expectedCodes = ['q5a', 'q5b'];
statusCodes.forEach((input, index) => {
if (input.value === answers[expectedCodes[index]]) {
score++;
input.classList.add('border-green-500');
} else {
input.classList.add('border-red-500');
}
});
// Show result
alert(`You scored ${score} out of ${totalQuestions}!`);
}
}
// Quiz answer checking function for day5.html
function checkAnswers() {
const currentPage = window.location.pathname.split('/').pop();
if (currentPage === 'day5.html') {
const answers = {
q1: 'A', // Basic Authentication
q2: 'C', // OAuth 2.0
q3: 'True', // Bearer tokens should be kept secret
q4: 'False', // OAuth 2.0 is for both authentication and authorization
q5a: 'Authorization', // Header name
q5b: '401' // Status code for invalid token
};
let score = 0;
const totalQuestions = 5;
// Check multiple choice and true/false
for (let i = 1; i <= 4; i++) {
const selected = document.querySelector(`input[name="q${i}"]:checked`);
if (selected) {
const answer = selected.parentElement.textContent.trim();
if (answer.startsWith(answers[`q${i}`])) {
score++;
selected.parentElement.classList.add('text-green-600');
} else {
selected.parentElement.classList.add('text-red-600');
}
}
}
// Check practical exercise
const headerInput = document.querySelector('input[type="text"]');
const statusInput = document.querySelector('input[type="number"]');
if (headerInput.value.trim().toLowerCase() === answers.q5a.toLowerCase()) {
score++;
headerInput.classList.add('border-green-500');
} else {
headerInput.classList.add('border-red-500');
}
if (statusInput.value === answers.q5b) {
score++;
statusInput.classList.add('border-green-500');
} else {
statusInput.classList.add('border-red-500');
}
// Show result
alert(`You scored ${score} out of ${totalQuestions}!`);
}
}
// Quiz answer checking function for day6.html
function checkAnswers() {
const currentPage = window.location.pathname.split('/').pop();
if (currentPage === 'day6.html') {
const answers = {
q1: 'B', // Content-Type
q2: 'B', // After a ? symbol
q3: 'True', // Accept header tells server what client can handle
q4: 'True', // Query parameters are case-sensitive
q5a: '5', // Number of comments for postId=1
q5b: 'application/json; charset=utf-8' // Content-Type header value
};
let score = 0;
const totalQuestions = 5;
// Check multiple choice and true/false
for (let i = 1; i <= 4; i++) {
const selected = document.querySelector(`input[name="q${i}"]:checked`);
if (selected) {
const answer = selected.parentElement.textContent.trim();
if (answer.startsWith(answers[`q${i}`])) {
score++;
selected.parentElement.classList.add('text-green-600');
} else {
selected.parentElement.classList.add('text-red-600');
}
}
}
// Check practical exercise
const numComments = document.querySelector('input[type="number"]').value;
const headerValue = document.querySelector('input[type="text"]').value.trim().toLowerCase();
if (numComments === answers.q5a) {
score++;
document.querySelector('input[type="number"]').classList.add('border-green-500');
} else {
document.querySelector('input[type="number"]').classList.add('border-red-500');
}
if (headerValue === answers.q5b) {
score++;
document.querySelector('input[type="text"]').classList.add('border-green-500');
} else {
document.querySelector('input[type="text"]').classList.add('border-red-500');
}
// Show result
alert(`You scored ${score} out of ${totalQuestions}!`);
}
}
// Quiz answer checking function for day7.html
function checkAnswers() {
const currentPage = window.location.pathname.split('/').pop();
if (currentPage === 'day7.html') {
const answers = {
q1: 'B', // To simulate API responses without a real backend
q2: 'C', // Both A and B
q3: 'True', // Mock APIs help frontend devs
q4: 'False', // Mock APIs do not return real data
q5a: '200', // Status code for successful GET
q5b: '{"message": "success"}' // Example JSON response (accept any non-empty)
};
let score = 0;
const totalQuestions = 5;
// Check multiple choice and true/false
for (let i = 1; i <= 4; i++) {
const selected = document.querySelector(`input[name="q${i}"]:checked`);
if (selected) {
const answer = selected.parentElement.textContent.trim();
if (answer.startsWith(answers[`q${i}`])) {
score++;
selected.parentElement.classList.add('text-green-600');
} else {
selected.parentElement.classList.add('text-red-600');
}
}
}
// Check practical exercise
const statusInput = document.querySelector('input[type="number"]');
const jsonInput = document.querySelector('input[type="text"]');
if (statusInput.value === answers.q5a) {
score++;
statusInput.classList.add('border-green-500');
} else {
statusInput.classList.add('border-red-500');
}
if (jsonInput.value.trim().length > 0) {
score++;
jsonInput.classList.add('border-green-500');
} else {
jsonInput.classList.add('border-red-500');
}
// Show result
alert(`You scored ${score} out of ${totalQuestions}!`);
}
}
// Quiz answer checking function for day8.html
function checkAnswers() {
const currentPage = window.location.pathname.split('/').pop();
if (currentPage === 'day8.html') {
const answers = {
q1: 'C', // Both A and B
q2: 'A', // Faster feedback and regression testing
q3: 'True', // Automated API tests can be run in CI/CD
q4: 'False', // Not only for large projects
q5a: '200', // Status code for GET /posts/1
q5b: 'sunt aut facere repellat provident occaecati excepturi optio reprehenderit' // Title of post 1
};
let score = 0;
const totalQuestions = 5;
// Check multiple choice and true/false
for (let i = 1; i <= 4; i++) {
const selected = document.querySelector(`input[name="q${i}"]:checked`);
if (selected) {
const answer = selected.parentElement.textContent.trim();
if (answer.startsWith(answers[`q${i}`])) {
score++;
selected.parentElement.classList.add('text-green-600');
} else {
selected.parentElement.classList.add('text-red-600');
}
}
}
// Check practical exercise
const statusInput = document.querySelector('input[type="number"]');
const titleInput = document.querySelector('input[type="text"]');
if (statusInput.value === answers.q5a) {
score++;
statusInput.classList.add('border-green-500');
} else {
statusInput.classList.add('border-red-500');
}
if (titleInput.value.trim() === answers.q5b) {
score++;
titleInput.classList.add('border-green-500');
} else {
titleInput.classList.add('border-red-500');
}
// Show result
alert(`You scored ${score} out of ${totalQuestions}!`);
}
}
// Quiz answer checking function for day9.html
function checkAnswers() {
const currentPage = window.location.pathname.split('/').pop();
if (currentPage === 'day9.html') {
const answers = {
q1: 'B', // 401 Unauthorized
q2: 'A', // Too Many Requests
q3: 'True', // 404 means not found
q4: 'False', // APIs should not always return 200 OK
q5a: '404', // Non-existent endpoint
q5b: '429' // Rate limit exceeded
};
let score = 0;
const totalQuestions = 5;
// Check multiple choice and true/false
for (let i = 1; i <= 4; i++) {
const selected = document.querySelector(`input[name="q${i}"]:checked`);
if (selected) {
const answer = selected.parentElement.textContent.trim();
if (answer.startsWith(answers[`q${i}`])) {
score++;
selected.parentElement.classList.add('text-green-600');
} else {
selected.parentElement.classList.add('text-red-600');
}
}
}
// Check practical exercise
const inputs = document.querySelectorAll('input[type="number"]');
const expectedCodes = ['q5a', 'q5b'];
inputs.forEach((input, index) => {
if (input.value === answers[expectedCodes[index]]) {
score++;
input.classList.add('border-green-500');
} else {
input.classList.add('border-red-500');
}
});
// Show result
alert(`You scored ${score} out of ${totalQuestions}!`);
}
}
// Quiz answer checking function for day10.html
function checkAnswers() {
const currentPage = window.location.pathname.split('/').pop();
if (currentPage === 'day10.html') {
const answers = {
q1: 'B', // PATCH
q2: 'C', // Both A and B
q3: 'False', // REST APIs do not have to use XML
q4: 'True', // 201 means created
q5a: '/users' // Endpoint for all users
};
let score = 0;
const totalQuestions = 5;
// Check multiple choice and true/false
for (let i = 1; i <= 4; i++) {
const selected = document.querySelector(`input[name="q${i}"]:checked`);
if (selected) {
const answer = selected.parentElement.textContent.trim();
if (answer.startsWith(answers[`q${i}`])) {
score++;
selected.parentElement.classList.add('text-green-600');
} else {
selected.parentElement.classList.add('text-red-600');
}
}
}
// Check practical exercise
const endpointInput = document.querySelector('input[type="text"]');
if (endpointInput.value.trim() === answers.q5a || endpointInput.value.trim() === 'https://api.example.com/users') {
score++;
endpointInput.classList.add('border-green-500');
} else {
endpointInput.classList.add('border-red-500');
}
// Show result
alert(`You scored ${score} out of ${totalQuestions}!`);
}
}