Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
603 lines (574 loc) · 25.4 KB
/
Copy pathscript.js
File metadata and controls
603 lines (574 loc) · 25.4 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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
// Modules data
const modules = [
{
id: 1,
title: "Intro to Python",
description: "Learn the basics of Python programming language, including syntax, variables, and basic operations.",
progress: 0,
unlocked: true,
challenges: [
{
type: "mcq",
question: "Which of the following is the correct way to print 'Hello, World!' in Python?",
options: ["print('Hello, World!')", "echo 'Hello, World!'", "console.log('Hello, World!')", "printf('Hello, World!')"],
correctAnswer: 0,
explanation: "In Python, the print() function is used to output text to the console."
},
{
type: "mcq",
question: "What symbol is used to start a comment in Python?",
options: ["//", "#", "--", "/*"],
correctAnswer: 1,
explanation: "In Python, comments start with the '#' symbol."
},
{
type: "mcq",
question: "Which of these is a valid variable name in Python?",
options: ["1name", "first_name", "first-name", "first name"],
correctAnswer: 1,
explanation: "Valid variable names can't start with numbers or contain spaces or hyphens."
},
{
type: "mcq",
question: "What is the output of: print(type(10))?",
options: ["class 'int'", "type 'int'", "int", "Integer"],
correctAnswer: 0,
explanation: "type(10) returns <class 'int'> in Python."
},
{
type: "mcq",
question: "Which keyword is used to define a function in Python?",
options: ["function", "def", "fun", "define"],
correctAnswer: 1,
explanation: "The 'def' keyword is used to define a function in Python."
}
]
},
{
id: 2,
title: "Data Types",
description: "Understand Python's built-in data types including integers, floats, strings, and booleans.",
progress: 0,
unlocked: false,
challenges: [
{
type: "mcq",
question: "Which of these is NOT a built-in data type in Python?",
options: ["int", "float", "string", "char"],
correctAnswer: 3,
explanation: "Python doesn't have a separate 'char' type. Single characters are strings of length 1."
},
{
type: "mcq",
question: "What data type is the result of: type(3.14)?",
options: ["int", "str", "float", "bool"],
correctAnswer: 2,
explanation: "3.14 is a float, so type(3.14) returns <class 'float'>."
},
{
type: "mcq",
question: "What is the output of: type(True)?",
options: ["class 'int'", "class 'bool'", "class 'str'", "class 'float'"],
correctAnswer: 1,
explanation: "True is a boolean value, so its type is <class 'bool'>."
},
{
type: "mcq",
question: "Which of the following would return a string?",
options: ["type('123')", "'123'", "int('123')", "123"],
correctAnswer: 1,
explanation: "Anything in quotes in Python is treated as a string."
},
{
type: "mcq",
question: "Which function converts a string to an integer in Python?",
options: ["str()", "chr()", "int()", "float()"],
correctAnswer: 2,
explanation: "int('42') converts the string '42' into the integer 42."
}
]
},
{
id: 3,
title: "Strings",
description: "Master string manipulation techniques including slicing, formatting, and common methods.",
progress: 0,
unlocked: false,
challenges: [
{
type: "mcq",
question: "What does the expression 'Hello'[1:4] return?",
options: ["ell", "Hel", "llo", "el"],
correctAnswer: 0,
explanation: "Slicing from index 1 to 4 (not including 4) gives 'ell'."
},
{
type: "mcq",
question: "Which method is used to convert a string to lowercase in Python?",
options: ["lower()", "down()", "toLower()", "case()"],
correctAnswer: 0,
explanation: "The correct method is lower()."
},
{
type: "mcq",
question: "'hello'.capitalize() returns:",
options: ["Hello", "HELLO", "hello", "hELLO"],
correctAnswer: 0,
explanation: "capitalize() makes the first letter uppercase, rest lowercase."
},
{
type: "mcq",
question: "What is the output of: len('Python')?",
options: ["5", "6", "7", "None"],
correctAnswer: 1,
explanation: "'Python' has 6 characters, so len('Python') returns 6."
},
{
type: "mcq",
question: "Which of the following methods finds a substring within a string?",
options: ["search()", "find()", "look()", "scan()"],
correctAnswer: 1,
explanation: "find() returns the index of the substring if found, or -1."
}
]
},
{
id: 4,
title: "Lists",
description: "Learn about Python lists, how to create, modify, and iterate through them.",
progress: 0,
unlocked: false,
challenges: [
{
type: "mcq",
question: "What is the output of list(range(3))?",
options: ["[0, 1, 2]", "[1, 2, 3]", "[0, 1, 2, 3]", "Error"],
correctAnswer: 0,
explanation: "range(3) generates 0 to 2, so list(range(3)) gives [0, 1, 2]."
},
{
type: "mcq",
question: "Which method is used to add an item to the end of a list?",
options: ["append()", "insert()", "add()", "push()"],
correctAnswer: 0,
explanation: "append() adds an item to the end of the list."
},
{
type: "mcq",
question: "What is the output of [1, 2] + [3, 4]?",
options: ["[1, 2, 3, 4]", "[1, 2][3, 4]", "Error", "[4, 5, 6]"],
correctAnswer: 0,
explanation: "The + operator concatenates the lists."
},
{
type: "mcq",
question: "How do you access the first element of a list called my_list?",
options: ["my_list[1]", "my_list[-1]", "my_list[0]", "my_list.first()"],
correctAnswer: 2,
explanation: "Lists are zero-indexed in Python, so first element is at index 0."
},
{
type: "mcq",
question: "Which method removes the first matching element from a list?",
options: ["pop()", "remove()", "delete()", "discard()"],
correctAnswer: 1,
explanation: "remove() deletes the first occurrence of the specified value."
}
]
},
{
id: 5,
title: "Dictionaries",
description: "Understand key-value pairs and how to work with Python dictionaries effectively.",
progress: 0,
unlocked: false,
challenges: [
{
type: "mcq",
question: "Which method is used to get all keys from a dictionary?",
options: ["keys()", "get_keys()", "allKeys()", "values()"],
correctAnswer: 0,
explanation: "keys() returns all dictionary keys."
},
{
type: "mcq",
question: "What happens if you access a non-existent key using square brackets?",
options: ["Returns None", "Raises KeyError", "Returns empty string", "Creates a new key"],
correctAnswer: 1,
explanation: "Accessing with [] on a non-existent key raises a KeyError."
},
{
type: "mcq",
question: "Which method safely gets a value from a dictionary?",
options: ["find()", "get()", "access()", "fetch()"],
correctAnswer: 1,
explanation: "get() returns None or a default value if key is missing."
},
{
type: "mcq",
question: "What is the output of len({'a': 1, 'b': 2})?",
options: ["1", "2", "3", "0"],
correctAnswer: 1,
explanation: "There are 2 key-value pairs, so len returns 2."
},
{
type: "mcq",
question: "What is the correct way to update a value in a dictionary?",
options: ["dict.update('key', 'new')", "dict['key'] = 'new'", "dict.add('key', 'new')", "dict.key = 'new'"],
correctAnswer: 1,
explanation: "Assigning directly to a key updates its value."
}
]
},
{
id: 6,
title: "Sets",
description: "Explore Python sets and their unique properties for handling collections of unique items.",
progress: 0,
unlocked: false,
challenges: [
{
type: "mcq",
question: "Which operation removes duplicates from a list?",
options: ["set()", "list()", "dict()", "tuple()"],
correctAnswer: 0,
explanation: "Converting a list to a set removes duplicates."
},
{
type: "mcq",
question: "Which method adds an element to a set?",
options: ["append()", "add()", "insert()", "extend()"],
correctAnswer: 1,
explanation: "add() inserts a new item into a set."
},
{
type: "mcq",
question: "What is the result of: len(set([1, 1, 2]))?",
options: ["3", "2", "1", "None"],
correctAnswer: 1,
explanation: "The set contains only unique elements: {1, 2}."
},
{
type: "mcq",
question: "Which of these methods checks if an element exists in a set?",
options: ["has()", "exists()", "in", "contains()"],
correctAnswer: 2,
explanation: "'in' is used to check membership in a set."
},
{
type: "mcq",
question: "What is the output of: set('aab')?",
options: ["{'a', 'a', 'b'}", "{'a', 'b'}", "['a', 'b']", "('a', 'b')"],
correctAnswer: 1,
explanation: "Duplicate 'a' is removed in set: {'a', 'b'}."
}
]
},
{
id: 7,
title: "Functions",
description: "Learn to create reusable code blocks with functions, parameters, and return values.",
progress: 0,
unlocked: false,
challenges: [
{
type: "mcq",
question: "Which keyword is used to define a function in Python?",
options: ["func", "def", "function", "lambda"],
correctAnswer: 1,
explanation: "Use 'def' to define a function in Python."
},
{
type: "mcq",
question: "What is the output of: def foo(): return 42; print(foo())",
options: ["foo", "None", "42", "Error"],
correctAnswer: 2,
explanation: "The function returns 42, so print(foo()) outputs 42."
},
{
type: "mcq",
question: "Which keyword is used to exit a function and return a value?",
options: ["break", "stop", "return", "exit"],
correctAnswer: 2,
explanation: "The return keyword exits the function with a value."
},
{
type: "mcq",
question: "What will `print(len('hello'))` return?",
options: ["4", "5", "6", "0"],
correctAnswer: 1,
explanation: "'hello' has 5 characters."
},
{
type: "mcq",
question: "What is the correct way to define a function with two parameters?",
options: ["def func(a, b):", "function func(a, b)", "func def(a, b):", "def func a, b:"],
correctAnswer: 0,
explanation: "The correct syntax is: def func(a, b):"
}
]
},
{
id: 8,
title: "Searching",
description: "Implement various searching algorithms in Python including linear and binary search.",
progress: 0,
unlocked: false,
challenges: [
{
type: "mcq",
question: "What is the time complexity of linear search in the worst case?",
options: ["O(1)", "O(log n)", "O(n)", "O(n log n)"],
correctAnswer: 2,
explanation: "Linear search checks every element."
},
{
type: "mcq",
question: "What condition must a list satisfy to use binary search?",
options: ["Must be sorted", "No duplicates", "Must be even length", "Only positive numbers"],
correctAnswer: 0,
explanation: "Binary search works only on sorted data."
},
{
type: "mcq",
question: "What does binary search reduce the array size by each time?",
options: ["1", "n", "Half", "Log n"],
correctAnswer: 2,
explanation: "Binary search divides the list into half each time."
},
{
type: "mcq",
question: "Which loop is commonly used in linear search?",
options: ["while", "do-while", "for", "switch"],
correctAnswer: 2,
explanation: "Linear search uses for loop over elements."
},
{
type: "mcq",
question: "Which algorithm is better for unsorted data?",
options: ["Binary search", "Linear search", "Hashing", "Sorting"],
correctAnswer: 1,
explanation: "Linear search works on unsorted data."
}
]
},
{
id: 9,
title: "Sorting",
description: "Master sorting algorithms like bubble sort, merge sort, and Python's built-in sorting.",
progress: 0,
unlocked: false,
challenges: [
{
type: "mcq",
question: "Which sorting algorithm has the best average-case time complexity?",
options: ["Bubble Sort", "Merge Sort", "Insertion Sort", "Selection Sort"],
correctAnswer: 1,
explanation: "Merge Sort has O(n log n) average-case complexity."
},
{
type: "mcq",
question: "Which built-in function sorts a list in Python?",
options: ["sort()", "sorted()", "order()", "arrange()"],
correctAnswer: 1,
explanation: "sorted() returns a new sorted list."
},
{
type: "mcq",
question: "Which sorting algorithm repeatedly swaps adjacent elements?",
options: ["Quick Sort", "Bubble Sort", "Selection Sort", "Merge Sort"],
correctAnswer: 1,
explanation: "Bubble Sort swaps neighbors until sorted."
},
{
type: "mcq",
question: "What is the worst-case complexity of Bubble Sort?",
options: ["O(n)", "O(n log n)", "O(n^2)", "O(1)"],
correctAnswer: 2,
explanation: "Bubble Sort worst-case is O(n²)."
},
{
type: "mcq",
question: "Which sorting algorithm is divide-and-conquer based?",
options: ["Bubble Sort", "Selection Sort", "Merge Sort", "Insertion Sort"],
correctAnswer: 2,
explanation: "Merge Sort splits the array recursively."
}
]
},
{
id: 10,
title: "Final Assessment",
description: "Combine all your Python knowledge in this comprehensive final challenge.",
progress: 0,
unlocked: false,
challenges: [
{
type: "mcq",
question: "Which keyword is used to begin a loop in Python?",
options: ["loop", "repeat", "for", "iterate"],
correctAnswer: 2,
explanation: "'for' is used to start a loop in Python."
},
{
type: "mcq",
question: "Which data type is used to store key-value pairs in Python?",
options: ["list", "tuple", "set", "dictionary"],
correctAnswer: 3,
explanation: "Dictionaries store key-value pairs."
},
{
type: "mcq",
question: "What will `3 ** 2` return?",
options: ["6", "9", "5", "Error"],
correctAnswer: 1,
explanation: "** is the exponent operator; 3**2 = 9."
},
{
type: "mcq",
question: "Which function is used to get user input?",
options: ["read()", "scan()", "input()", "get()"],
correctAnswer: 2,
explanation: "input() reads a line of user input."
},
{
type: "mcq",
question: "What does `type([])` return?",
options: ["list", "<class 'list'>", "[]", "array"],
correctAnswer: 1,
explanation: "It returns <class 'list'>."
}
]
}
];
// DOM Elements
const modulesContainer = document.getElementById('modulesContainer');
const modalOverlay = document.getElementById('modalOverlay');
const closeModal = document.getElementById('closeModal');
const modalTitle = document.getElementById('modalTitle');
const modalBody = document.getElementById('modalBody');
let currentModule = null;
let currentChallengeIndex = 0;
let completedChallenges = {}; // 👈 NEW
function init() {
renderModules();
}
function renderModules() {
modulesContainer.innerHTML = '';
modules.forEach(module => {
const moduleCard = document.createElement('div');
moduleCard.className = 'module-card';
moduleCard.innerHTML = `
<div class="module-header">
<div class="module-number">${module.id}</div>
<h3 class="module-title">${module.title}</h3>
</div>
<div class="module-body">
<p class="module-description">${module.description}</p>
<div class="module-stats">
<span>${module.progress}% Complete</span>
<span>${module.challenges.length} Challenges</span>
</div>
<div class="module-progress">
<div class="module-progress-fill" style="width: ${module.progress}%"></div>
</div>
<div class="module-actions">
<button class="btn btn-outline"><i class="fas fa-info-circle btn-icon"></i>Details</button>
<button class="btn btn-primary start-challenge" data-module="${module.id}">
<i class="fas fa-play btn-icon"></i>Start
</button>
</div>
</div>
${!module.unlocked ? `
<div class="locked-overlay">
<i class="fas fa-lock locked-icon"></i>
<div class="locked-text">Complete previous modules to unlock</div>
</div>
` : ''}
`;
modulesContainer.appendChild(moduleCard);
});
setupEventListeners();
}
function setupEventListeners() {
document.querySelectorAll('.start-challenge').forEach(button => {
button.addEventListener('click', function () {
const moduleId = parseInt(this.getAttribute('data-module'));
startModule(moduleId);
});
});
closeModal.addEventListener('click', closeChallengeModal);
modalOverlay.addEventListener('click', function (e) {
if (e.target === modalOverlay) closeChallengeModal();
});
}
function startModule(moduleId) {
currentModule = modules.find(m => m.id === moduleId);
if (!currentModule || !currentModule.unlocked) return;
if (currentModule.challenges.length === 0) {
alert('This module has no challenges yet. Coming soon!');
return;
}
currentChallengeIndex = 0;
showChallenge(currentChallengeIndex);
}
function showChallenge(index) {
if (!currentModule || index >= currentModule.challenges.length) {
closeChallengeModal();
return;
}
const challenge = currentModule.challenges[index];
modalTitle.textContent = `${currentModule.title} - Challenge ${index + 1}`;
modalBody.innerHTML = `
<div class="challenge-question">
<h3 class="challenge-title">Multiple Choice Question</h3>
<p class="challenge-description">${challenge.question}</p>
</div>
<div class="options-container" id="optionsContainer">
${challenge.options.map((option, i) => `
<div class="option" data-answer="${i}">${option}</div>
`).join('')}
</div>
<div id="feedback" style="display: none; padding: 15px; border-radius: 5px; margin-bottom: 20px;"></div>
<button class="btn btn-primary" id="nextChallenge" style="display: none;">
Next Challenge <i class="fas fa-arrow-right"></i>
</button>
`;
modalOverlay.classList.add('active');
const optionsContainer = document.getElementById('optionsContainer');
const feedback = document.getElementById('feedback');
const nextBtn = document.getElementById('nextChallenge');
optionsContainer.addEventListener('click', function (e) {
if (e.target.classList.contains('option')) {
const selectedAnswer = parseInt(e.target.getAttribute('data-answer'));
const isCorrect = selectedAnswer === challenge.correctAnswer;
feedback.textContent = isCorrect
? `✅ Correct! ${challenge.explanation}`
: `❌ Incorrect. ${challenge.explanation}`;
feedback.style.display = 'block';
feedback.style.backgroundColor = isCorrect ? 'rgba(40, 167, 69, 0.1)' : 'rgba(220, 53, 69, 0.1)';
feedback.style.color = isCorrect ? 'green' : 'red';
nextBtn.style.display = 'inline-block';
}
});
nextBtn.addEventListener('click', function () {
const modId = currentModule.id;
if (!completedChallenges[modId]) completedChallenges[modId] = 0;
completedChallenges[modId]++;
if (completedChallenges[modId] >= currentModule.challenges.length) {
const nextModule = modules.find(m => m.id === modId + 1);
if (nextModule && !nextModule.unlocked) {
nextModule.unlocked = true;
alert(`🎉 Great job! You've unlocked "${nextModule.title}"`);
}
renderModules();
}
currentChallengeIndex++;
showChallenge(currentChallengeIndex);
});
}
function closeChallengeModal() {
modalOverlay.classList.remove('active');
modalBody.innerHTML = '';
}
document.addEventListener('DOMContentLoaded', init);