-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.html
More file actions
1716 lines (1424 loc) · 62.9 KB
/
test.html
File metadata and controls
1716 lines (1424 loc) · 62.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
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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>JavaScript Practice - Learn by Doing</title>
<meta name="robots" content="noindex, nofollow">
<style>
::-webkit-scrollbar {
display: none;
}
* {
-webkit-tap-highlight-color: transparent;
-webkit-touch-callout: none;
-webkit-user-select: none;
-ms-user-select: none;
user-select: none;
}
:root {
--accent: #4caf50;
--dark-bg: #2b2b2b;
--light-bg: #f9f9f9;
}
body {
font-family: 'Segoe UI', sans-serif;
margin: 0;
padding: 0;
background: var(--light-bg);
color: #333;
}
body.dark,
body.dark .container,
body.dark .drawer,
body.dark .lesson-section,
body.dark #dropdownToggle,
body.dark .dropdown-menu,
body.dark textarea,
body.dark pre {
background: #1e1e1e;
color: #eee;
}
body.dark textarea,
body.dark pre {
background: #2b2b2b;
}
header {
background: var(--dark-bg);
color: white;
padding: 1rem 1.25rem;
border-bottom: 4px solid var(--accent);
}
.header-wrapper {
display: flex;
align-items: center;
justify-content: space-between;
flex-wrap: wrap;
position: relative;
}
.logo-title {
display: flex;
align-items: center;
gap: 0.75rem;
flex: 1;
min-width: 0;
}
.logo {
width: 42px;
height: 42px;
border-radius: 50%;
object-fit: cover;
cursor: pointer;
}
.logo-title .text h1 {
font-size: 1.4rem;
margin: 0;
}
.logo-title .text p {
font-size: 0.9rem;
margin: 0;
opacity: 0.85;
}
.theme-btn,
.hamburger {
z-index: 999;
background: transparent;
font-size: 1rem;
border-radius: 6px;
cursor: pointer;
color: white;
padding: 0.4rem 0.75rem;
}
.theme-btn {
border: 1px solid white;
}
.hamburger {
border: 1px solid rgb(7, 249, 23);
font-size: 1.2rem;
padding: 0.3rem 0.6rem;
}
.container {
display: flex;
flex-direction: row;
height: calc(100vh - 96px);
overflow: hidden;
}
.drawer {
position: fixed;
top: 96px;
left: -100%;
width: 85%;
max-width: 300px;
height: calc(100% - 96px);
background: #fff;
box-shadow: 2px 0 10px rgba(0, 0, 0, 0.1);
z-index: 10;
transition: left 0.3s ease;
overflow-y: auto;
padding: 1rem;
}
.drawer.open {
left: 0;
top: 130px;
overflow-y: scroll;
}
.lesson h2,
.editor h2 {
margin-top: 0;
}
.lesson pre {
background: #eee;
padding: 0.75rem;
border-radius: 6px;
overflow-x: auto;
position: relative;
}
.editor {
flex: 1;
padding: 1rem 2rem;
overflow-y: auto;
}
.editor-group {
display: flex;
flex-direction: column;
gap: 1rem;
}
.copy-btn {
position: absolute;
top: 8px;
right: 8px;
padding: 4px 8px;
background: #4caf50;
border: none;
color: white;
border-radius: 4px;
font-size: 0.8rem;
cursor: pointer;
}
.lesson-section {
background: #fff;
border-radius: 10px;
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
margin-bottom: 2rem;
}
.lesson-section h2 {
font-size: 1.4rem;
color: var(--accent);
margin-bottom: 0.5rem;
}
textarea {
width: 100%;
height: 220px;
font-family: monospace;
font-size: 1rem;
padding: 0.75rem;
border-radius: 6px;
background: #282c34;
color: #f8f8f2;
border: none;
box-shadow: inset 0 0 8px rgba(0, 0, 0, 0.2);
resize: vertical;
}
button#runBtn {
margin-top: 1rem;
padding: 0.5rem 1rem;
background-color: var(--accent);
color: white;
border: none;
border-radius: 6px;
cursor: pointer;
font-size: 1rem;
}
iframe#preview {
margin-top: 1rem;
width: 100%;
height: 300px;
border: 1px solid #ccc;
background: white;
border-radius: 6px;
}
/* Breakpoints */
@media (min-width: 768px) {
.hamburger {
display: none;
}
.drawer {
position: relative;
left: 0 !important;
top: 0;
width: 40%;
box-shadow: none;
padding: 2rem;
}
.editor {
width: 60%;
}
.editor-group {
flex-direction: row;
}
}
@media (max-width: 768px) {
.header-wrapper {
flex-direction: column;
align-items: flex-start;
gap: 0.75rem;
}
.logo-title {
justify-content: flex-start;
}
.theme-btn {
align-self: flex-end;
}
.hamburger {
align-self: flex-start;
display: block;
}
.editor {
padding: 1rem 2rem 1rem 1rem;
}
#runBtn {
position: fixed;
bottom: 1rem;
right: 1rem;
z-index: 999;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
}
.btn-wrapper {
width: 100%;
display: flex;
justify-content: space-between;
}
}
/* Dropdown Filter */
.dropdown {
position: relative;
margin-bottom: 1rem;
}
#dropdownToggle {
padding: 0.6rem 1rem;
border: 1px solid #ccc;
background: #f9f9f9;
border-radius: 6px;
cursor: pointer;
font-size: 1rem;
width: 100%;
text-align: left;
}
.dropdown-menu {
position: absolute;
top: 100%;
left: 0;
z-index: 10;
background: white;
border: 1px solid #ddd;
border-radius: 6px;
padding: 1rem;
margin-top: 0.4rem;
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
display: none;
max-height: 300px;
overflow-y: auto;
width: 100%;
}
.dropdown-menu label {
display: block;
margin-bottom: 0.4rem;
font-size: 0.95rem;
}
.dropdown-actions {
display: flex;
gap: 0.5rem;
margin-bottom: 0.75rem;
}
.dropdown-actions button {
padding: 0.3rem 0.6rem;
font-size: 0.9rem;
cursor: pointer;
border: 1px solid #ccc;
background: #eee;
border-radius: 4px;
}
.dropdown.open .dropdown-menu {
display: block;
}
@keyframes floatSpin {
0% {
transform: rotate(0deg) scale(1);
opacity: 1;
}
50% {
transform: rotate(180deg) scale(1.05);
opacity: 0.9;
}
100% {
transform: rotate(360deg) scale(1);
opacity: 1;
}
}
.logo.logo-animate {
animation: floatSpin 8s ease-in-out infinite;
transform-origin: center;
will-change: transform, opacity;
}
</style>
</head>
<body>
<header>
<div class="header-wrapper">
<div class="logo-title">
<img src="./images/bmc-it-club-circle-logo.png" alt="Logo" class="logo logo-animate"
onclick="window.location.href = './index.html'" />
<div class="text">
<h1>Practice CSS</h1>
<p>Write and run real code. No setup required.</p>
</div>
</div>
<div class="btn-wrapper">
<button class="hamburger" id="hamburgerBtn">☰</button>
<button id="toggleTheme" class="theme-btn">🌗</button>
</div>
</div>
</header>
<div class="container">
<!-- Sliding Drawer -->
<aside class="drawer" id="drawer">
<section class="lesson" id="lessonSection">
<div id="filterDropdown" class="dropdown">
<button id="dropdownToggle">Filter Topics</button>
<div id="dropdownMenu" class="dropdown-menu">
<div class="dropdown-actions">
<button type="button" id="selectAllBtn">Select All</button>
<button type="button" id="clearAllBtn">Remove All</button>
</div>
<div id="topicCheckboxes">
<label><input type="checkbox" value="variables" checked> Variables</label>
<label><input type="checkbox" value="data-types" checked> Data Types</label>
<label><input type="checkbox" value="conditionals" checked> Conditionals</label>
<label><input type="checkbox" value="loops" checked> Loops</label>
<label><input type="checkbox" value="functions" checked> Functions</label>
<label><input type="checkbox" value="arrays" checked> Arrays</label>
<label><input type="checkbox" value="objects" checked> Objects</label>
<label><input type="checkbox" value="dom" checked> DOM Manipulation</label>
<label><input type="checkbox" value="dom-style-manipulation" checked> DOM Style
Manipulation</label>
<label><input type="checkbox" value="events" checked> Events</label>
<label><input type="checkbox" value="es6" checked> ES6 Features</label>
<label><input type="checkbox" value="async" checked> Async</label>
<label><input type="checkbox" value="fetch-api" checked> API</label>
<label><input type="checkbox" value="modules" checked> Modules</label>
<label><input type="checkbox" value="event-loop" checked> Event Loop</label>
</div>
</div>
</div>
<div class="lesson-section" data-topic="variables">
<h2>JavaScript Variables</h2>
<p>
Variables are used to store data in JavaScript so you can use and manipulate it later. Think of
a variable like a
labeled box that holds information such as numbers, text, or other types of data.
</p>
<h3>Types of Variable Declarations</h3>
<ul>
<li><strong>var</strong>: Old way of declaring variables (function-scoped, can be re-declared).
</li>
<li><strong>let</strong>: Modern way to declare variables (block-scoped, can be updated).</li>
<li><strong>const</strong>: Block-scoped but <strong>cannot be reassigned</strong> once defined.
</li>
</ul>
<h3>Declaring Variables</h3>
<pre><code>// Using var
var name = "Alice";
// Using let
let age = 25;
// Using const
const country = "Nepal";</code><button class="copy-btn">Copy</button></pre>
<h3>Reassigning Values</h3>
<pre><code>let score = 10;
score = 20; // OK
const year = 2024;
year = 2025; // ❌ Error: Cannot reassign a const</code><button class="copy-btn">Copy</button></pre>
<h3>Variable Naming Rules</h3>
<ul>
<li>Must start with a letter, <code>_</code> or <code>$</code>.</li>
<li>Cannot start with a number.</li>
<li>No spaces allowed.</li>
<li>Use camelCase for multiple words: <code>userName</code></li>
</ul>
<h3>Example</h3>
<pre><code>let userName = "John";
let userAge = 30;
console.log("Name:", userName);
console.log("Age:", userAge);</code><button class="copy-btn">Copy</button></pre>
<h3>Output</h3>
<pre><code>Name: John
Age: 30</code><button class="copy-btn">Copy</button></pre>
<h3>Live HTML + JS Example</h3>
<pre><code><script>
let product = "Laptop";
let price = 1200;
alert("Product: " + product + "\\nPrice: $" + price);
</script></code><button class="copy-btn">Copy</button></pre>
</div>
<div class="lesson-section" data-topic="data-types">
<h2>JavaScript Data Types</h2>
<p>
Data types define the kind of value a variable can hold. JavaScript is a dynamically typed
language, meaning you don’t have to specify the type — it figures it out for you.
</p>
<h3>Primitive Data Types</h3>
<ul>
<li><strong>String</strong>: Text data → <code>"hello"</code> or <code>'world'</code></li>
<li><strong>Number</strong>: Numeric values → <code>42</code>, <code>3.14</code></li>
<li><strong>Boolean</strong>: True or false → <code>true</code>, <code>false</code></li>
<li><strong>Undefined</strong>: A variable declared but not assigned → <code>let a;</code></li>
<li><strong>Null</strong>: Explicitly empty → <code>null</code></li>
<li><strong>BigInt</strong>: Large integers → <code>123456789012345678901234567890n</code></li>
<li><strong>Symbol</strong>: Unique identifier (advanced) → <code>Symbol("id")</code></li>
</ul>
<h3>Non-Primitive (Reference) Types</h3>
<ul>
<li><strong>Object</strong>: Key-value pairs → <code>{ name: "Alex", age: 30 }</code></li>
<li><strong>Array</strong>: Ordered list of values → <code>[1, 2, 3]</code></li>
<li><strong>Function</strong>: Reusable block of code → <code>function greet() { ... }</code>
</li>
</ul>
<h3>Examples</h3>
<pre><code>let name = "Sita"; // String
let age = 22; // Number
let isStudent = true; // Boolean
let nothing = null; // Null
let notDefined; // Undefined
let big = 1234567890123n; // BigInt
let person = { name: "Ram", age: 25 }; // Object
let colors = ["red", "green", "blue"]; // Array
let greet = function() {
console.log("Hello!");
}; // Function</code><button class="copy-btn">Copy</button></pre>
<h3>Checking Type</h3>
<p>You can check a variable’s type using the <code>typeof</code> operator.</p>
<pre><code>console.log(typeof "Hello"); // string
console.log(typeof 123); // number
console.log(typeof true); // boolean
console.log(typeof {}); // object
console.log(typeof []); // object (yes, arrays are objects!)
console.log(typeof null); // object (this is a historical bug)</code><button class="copy-btn">Copy</button></pre>
</div>
<div class="lesson-section" data-topic="conditionals">
<h2>JavaScript Conditionals</h2>
<p>
Conditional statements allow you to run different blocks of code based on different conditions.
They're how your code makes decisions.
</p>
<h3>Types of Conditional Statements</h3>
<ul>
<li><strong>if</strong>: Runs a block of code if a condition is true</li>
<li><strong>else</strong>: Runs if the condition is false</li>
<li><strong>else if</strong>: Checks another condition if the previous one is false</li>
<li><strong>switch</strong>: Selects one of many blocks of code to run</li>
<li><strong>ternary operator</strong>: A shortcut for if...else</li>
</ul>
<h3>Examples</h3>
<pre><code>// Basic if
let score = 85;
if (score > 80) {
console.log("Great job!");
}
// if...else
let age = 16;
if (age >= 18) {
console.log("You can vote.");
} else {
console.log("You are too young to vote.");
}
// if...else if...else
let time = 14;
if (time < 12) {
console.log("Good morning!");
} else if (time < 18) {
console.log("Good afternoon!");
} else {
console.log("Good evening!");
}
// switch statement
let color = "blue";
switch (color) {
case "red":
console.log("Color is red");
break;
case "blue":
console.log("Color is blue");
break;
default:
console.log("Color not recognized");
}
// ternary operator
let isLoggedIn = true;
let message = isLoggedIn ? "Welcome back!" : "Please log in.";
console.log(message);</code><button class="copy-btn">Copy</button></pre>
<h3>Real Example</h3>
<pre><code><script>
let temperature = 30;
if (temperature > 35) {
alert("It’s really hot outside!");
} else if (temperature > 20) {
alert("Nice weather.");
} else {
alert("It’s a bit cold.");
}
</script></code><button class="copy-btn">Copy</button></pre>
</div>
<div class="lesson-section" data-topic="loops">
<h2>JavaScript Loops</h2>
<p>
Loops are used to repeat a block of code multiple times. They help reduce repetition and make
code more efficient.
</p>
<h3>1. <code>for</code> Loop</h3>
<p>Used when you know how many times to loop.</p>
<pre><code>// For loop
for (let i = 1; i <= 5; i++) {
console.log("Count:", i);
}</code><button class="copy-btn">Copy</button></pre>
<h3>2. <code>while</code> Loop</h3>
<p>Runs as long as the condition is true. Good when the number of iterations is not known
beforehand.</p>
<pre><code>// While loop
let x = 1;
while (x <= 3) {
console.log("While Loop:", x);
x++;
}</code><button class="copy-btn">Copy</button></pre>
<h3>3. <code>do...while</code> Loop</h3>
<p>Runs the code block once before checking the condition. It always runs at least once.</p>
<pre><code>// Do...while loop
let y = 1;
do {
console.log("Do While Loop:", y);
y++;
} while (y <= 2);</code><button class="copy-btn">Copy</button></pre>
<h3>4. <code>for...of</code> Loop</h3>
<p>Used to loop through the values in an iterable (like arrays, strings, etc.).</p>
<pre><code>// For...of loop
let fruits = ["apple", "banana", "cherry"];
for (let fruit of fruits) {
console.log("Fruit:", fruit);
}</code><button class="copy-btn">Copy</button></pre>
<h3>5. <code>for...in</code> Loop</h3>
<p>Used to loop through the properties (keys) of an object.</p>
<pre><code>// For...in loop
let user = { name: "Alex", age: 20 };
for (let key in user) {
console.log(key + ":", user[key]);
}</code><button class="copy-btn">Copy</button></pre>
<h3>Real Example</h3>
<pre><code><script>
let colors = ["red", "green", "blue"];
let list = "";
for (let i = 0; i < colors.length; i++) {
list += colors[i] + " ";
}
alert("Colors: " + list);
</script></code><button class="copy-btn">Copy</button></pre>
</div>
<div class="lesson-section" data-topic="functions">
<h2>JavaScript Functions</h2>
<p>
A <strong>function</strong> is a block of reusable code that performs a specific task. Functions
help break code into smaller, manageable pieces.
</p>
<h3>Why Use Functions?</h3>
<ul>
<li>Write code once and reuse it.</li>
<li>Keep your code organized and clean.</li>
<li>Make large programs easier to manage.</li>
</ul>
<h3>Function Structure</h3>
<pre><code>function functionName(parameters) {
// code to run
}</code><button class="copy-btn">Copy</button></pre>
<h3>1. Declaring and Calling a Function</h3>
<p>Create a function using <code>function</code>, and then call it by its name followed by
<code>()</code>.
</p>
<pre><code>// Declare the function
function greet() {
console.log("Hello there!");
}
// Call the function
greet();</code><button class="copy-btn">Copy</button></pre>
<h3>2. Function with Parameters</h3>
<p>You can pass values into a function using parameters (placeholders).</p>
<pre><code>function greetUser(name) {
console.log("Hello, " + name + "!");
}
greetUser("Alex"); // Hello, Alex!</code><button class="copy-btn">Copy</button></pre>
<h3>3. Function with Return Value</h3>
<p>Use <code>return</code> to send a value back from a function.</p>
<pre><code>function add(a, b) {
return a + b;
}
let sum = add(5, 3); // sum is 8
console.log(sum);</code><button class="copy-btn">Copy</button></pre>
<h3>4. Function Expression</h3>
<p>You can also assign a function to a variable.</p>
<pre><code>const multiply = function(x, y) {
return x * y;
};
console.log(multiply(4, 3));</code><button class="copy-btn">Copy</button></pre>
<h3>5. Arrow Functions (Shorter Syntax)</h3>
<p>Arrow functions are shorter versions of function expressions.</p>
<pre><code>const square = (n) => {
return n * n;
};
console.log(square(5));</code><button class="copy-btn">Copy</button></pre>
<h3>6. Real Example</h3>
<p>Calculate area of a rectangle:</p>
<pre><code><script>
function getArea(width, height) {
return width * height;
}
let area = getArea(10, 5);
alert("Area is " + area);
</script></code><button class="copy-btn">Copy</button></pre>
<h3>Tips for Beginners</h3>
<ul>
<li>Always start with <code>function</code> keyword (unless using arrow functions).</li>
<li>You can create multiple functions and call them as needed.</li>
<li><strong>Parameters</strong> are like input; <strong>return</strong> gives output.</li>
<li>Try to write small, focused functions for one task only.</li>
</ul>
</div>
<div class="lesson-section" data-topic="arrays">
<h2>JavaScript Arrays & Array Methods</h2>
<p>
An <strong>array</strong> is a list of items stored in a single variable. It's useful when you
want to group similar data.
</p>
<h3>1. Creating Arrays</h3>
<pre><code>let fruits = ["apple", "banana", "orange"];
let numbers = [1, 2, 3, 4, 5];</code><button class="copy-btn">Copy</button></pre>
<h3>2. Accessing Items</h3>
<p>Array items start at index <code>0</code>.</p>
<pre><code>console.log(fruits[0]); // "apple"</code><button class="copy-btn">Copy</button></pre>
<h3>3. Changing Items</h3>
<pre><code>fruits[1] = "mango";
console.log(fruits); // ["apple", "mango", "orange"]</code><button class="copy-btn">Copy</button></pre>
<h3>4. Adding & Removing Items</h3>
<pre><code>fruits.push("grape"); // Add at end
fruits.pop(); // Remove last
fruits.unshift("kiwi"); // Add at beginning
fruits.shift(); // Remove first</code><button class="copy-btn">Copy</button></pre>
<h3>5. Looping Through Arrays</h3>
<pre><code>fruits.forEach(function(fruit) {
console.log(fruit);
});</code><button class="copy-btn">Copy</button></pre>
<hr>
<h2>Array Methods (React Favorites)</h2>
<h3>1. map()</h3>
<p><code>map()</code> creates a new array by transforming each item.</p>
<pre><code>let nums = [1, 2, 3];
let doubled = nums.map(n => n * 2);
console.log(doubled); // [2, 4, 6]</code><button class="copy-btn">Copy</button></pre>
<h3>2. filter()</h3>
<p><code>filter()</code> creates a new array with only items that match a condition.</p>
<pre><code>let nums = [1, 2, 3, 4, 5];
let even = nums.filter(n => n % 2 === 0);
console.log(even); // [2, 4]</code><button class="copy-btn">Copy</button></pre>
<h3>3. find()</h3>
<p><code>find()</code> returns the <strong>first</strong> item that matches a condition.</p>
<pre><code>let users = [
{ name: "Alice", age: 20 },
{ name: "Bob", age: 25 }
];
let found = users.find(user => user.age === 25);
console.log(found); // { name: "Bob", age: 25 }</code><button class="copy-btn">Copy</button></pre>
<h3>4. includes()</h3>
<p><code>includes()</code> checks if an item exists in an array.</p>
<pre><code>let colors = ["red", "blue", "green"];
console.log(colors.includes("blue")); // true</code><button class="copy-btn">Copy</button></pre>
<h3>5. reduce()</h3>
<p><code>reduce()</code> turns an array into a single value (like sum).</p>
<pre><code>let prices = [10, 20, 30];
let total = prices.reduce((sum, price) => sum + price, 0);
console.log(total); // 60</code><button class="copy-btn">Copy</button></pre>
<h3>Bonus: Chaining Methods</h3>
<pre><code>let result = [1, 2, 3, 4, 5]
.filter(n => n % 2 === 0)
.map(n => n * 10);
console.log(result); // [20, 40]</code><button class="copy-btn">Copy</button></pre>
<h3>Why this matters for React</h3>
<ul>
<li><code>map()</code> is used to render lists of components.</li>
<li><code>filter()</code> and <code>find()</code> help handle dynamic data.</li>
<li><code>includes()</code> is used for checking selections.</li>
<li><code>reduce()</code> is helpful for totals, scores, etc.</li>
</ul>
</div>
<div class="lesson-section" data-topic="objects">
<h2>JavaScript Objects</h2>
<p>
An <strong>object</strong> is a collection of key-value pairs (properties). It's used to store
related data and functions in a structured way.
</p>
<h3>1. Creating an Object</h3>
<pre><code>const person = {
name: "Alice",
age: 25,
isStudent: true
};</code><button class="copy-btn">Copy</button></pre>
<h3>2. Accessing Properties</h3>
<p>You can access values using dot <code>.</code> or bracket <code>[]</code> notation:</p>
<pre><code>console.log(person.name); // "Alice"
console.log(person["age"]); // 25</code><button class="copy-btn">Copy</button></pre>
<h3>3. Changing & Adding Properties</h3>
<pre><code>person.age = 26; // Change
person.city = "New York"; // Add
console.log(person);</code><button class="copy-btn">Copy</button></pre>
<h3>4. Deleting a Property</h3>
<pre><code>delete person.isStudent;</code><button class="copy-btn">Copy</button></pre>
<h3>5. Nested Objects</h3>
<p>Objects can hold other objects as values.</p>
<pre><code>const user = {
name: "Bob",
address: {
city: "Kathmandu",
zip: 44600
}
};
console.log(user.address.city); // "Kathmandu"</code><button class="copy-btn">Copy</button></pre>
<h3>6. Object Destructuring</h3>
<p>Destructuring lets you extract values from an object easily.</p>
<pre><code>const person = {
name: "Alice",
age: 25,
city: "Pokhara"
};
// Destructure properties
const { name, age } = person;
console.log(name); // "Alice"
console.log(age); // 25</code><button class="copy-btn">Copy</button></pre>
<h3>7. Nested Destructuring</h3>
<pre><code>const user = {
name: "Bob",
address: {
city: "Birgunj",
zip: 44300
}
};
const { address: { city } } = user;
console.log(city); // "Birgunj"</code><button class="copy-btn">Copy</button></pre>
<h3>Bonus: Object in Arrays (Common in React)</h3>
<pre><code>const people = [
{ name: "Ram", age: 22 },
{ name: "Sita", age: 28 }
];
people.forEach(person => {
console.log(person.name + " is " + person.age + " years old.");
});</code><button class="copy-btn">Copy</button></pre>
<p><strong>Why this matters in React:</strong> You'll often deal with objects (like user data, form
state, API responses), and destructuring makes your code clean and readable!</p>
</div>
<div class="lesson-section" data-topic="dom">
<h2>DOM Manipulation</h2>
<p>
DOM (Document Object Model) manipulation is how JavaScript interacts with and updates HTML
elements on a page.
You can select elements, change their content or style, add/remove elements, and handle events.
</p>
<h3>Selecting Elements</h3>
<ul>
<li><code>document.getElementById("id")</code> – Select by ID</li>
<li><code>document.querySelector("selector")</code> – Select first match</li>
<li><code>document.querySelectorAll("selector")</code> – Select all matches</li>
<li><code>document.getElementsByClassName("class")</code> – Old method (HTMLCollection)</li>
<li><code>document.getElementsByTagName("tag")</code> – Old method (HTMLCollection)</li>
</ul>
<h3>Example HTML</h3>
<pre><code><h1 id="title">Original Title</h1>
<p id="info">Some info</p>
<div class="box">Box content</div>
<ul><li>Item 1</li></ul>
<button id="myBtn">Click Me</button></code><button class="copy-btn">Copy</button></pre>
<h3>Changing Content</h3>
<pre><code>// Change text
document.getElementById("title").textContent = "Hello!";
// Change HTML
document.getElementById("info").innerHTML = "<strong>Updated!</strong>";</code><button class="copy-btn">Copy</button></pre>
<h3>Changing Styles</h3>
<pre><code>const box = document.querySelector(".box");
box.style.backgroundColor = "skyblue";
box.style.padding = "20px";</code><button class="copy-btn">Copy</button></pre>
<h3>Creating & Appending Elements</h3>
<pre><code>const newItem = document.createElement("li");
newItem.textContent = "New list item";
document.querySelector("ul").appendChild(newItem);</code><button class="copy-btn">Copy</button></pre>
<h3>Removing Elements</h3>
<pre><code>const item = document.querySelector("li");
item.remove();</code><button class="copy-btn">Copy</button></pre>
<h3>Event Listeners</h3>
<pre><code>const btn = document.getElementById("myBtn");
btn.addEventListener("click", function () {
alert("Button clicked!");
});</code><button class="copy-btn">Copy</button></pre>
</div>
<div class="lesson-section" data-topic="dom-style-manipulation">
<h2>DOM: Styling & Element Manipulation</h2>
<p>
JavaScript can change the appearance and structure of your web page by modifying styles,
classes, IDs, and HTML elements.
</p>
<h3>1. Changing Inline Styles</h3>
<pre><code>const box = document.getElementById("myBox");
// Add or update styles
box.style.backgroundColor = "lightblue";
box.style.padding = "1rem";
// Remove a style
box.style.backgroundColor = "";</code><button class="copy-btn">Copy</button></pre>
<pre><code><div id="myBox">Styled Box</div></code><button class="copy-btn">Copy</button></pre>
<h3>2. Working with Classes</h3>
<pre><code>const btn = document.querySelector("button");
// Add a class
btn.classList.add("active");
// Remove a class
btn.classList.remove("active");