-
Notifications
You must be signed in to change notification settings - Fork 0
/
02_control_structure_notes.html
1357 lines (1337 loc) · 109 KB
/
02_control_structure_notes.html
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 xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
<meta charset="utf-8">
<meta name="generator" content="quarto-1.5.45">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<title>control_structure_notes</title>
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
div.columns{display: flex; gap: min(4vw, 1.5em);}
div.column{flex: auto; overflow-x: auto;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
ul.task-list li input[type="checkbox"] {
width: 0.8em;
margin: 0 0.8em 0.2em -1em; /* quarto-specific, see https://github.com/quarto-dev/quarto-cli/issues/4556 */
vertical-align: middle;
}
/* CSS for syntax highlighting */
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { display: inline-block; text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
}
pre.numberSource { margin-left: 3em; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
</style>
<script src="02_control_structure_notes_files/libs/clipboard/clipboard.min.js"></script>
<script src="02_control_structure_notes_files/libs/quarto-html/quarto.js"></script>
<script src="02_control_structure_notes_files/libs/quarto-html/popper.min.js"></script>
<script src="02_control_structure_notes_files/libs/quarto-html/tippy.umd.min.js"></script>
<script src="02_control_structure_notes_files/libs/quarto-html/anchor.min.js"></script>
<link href="02_control_structure_notes_files/libs/quarto-html/tippy.css" rel="stylesheet">
<link href="02_control_structure_notes_files/libs/quarto-html/quarto-syntax-highlighting.css" rel="stylesheet" id="quarto-text-highlighting-styles">
<script src="02_control_structure_notes_files/libs/bootstrap/bootstrap.min.js"></script>
<link href="02_control_structure_notes_files/libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
<link href="02_control_structure_notes_files/libs/bootstrap/bootstrap.min.css" rel="stylesheet" id="quarto-bootstrap" data-mode="light">
</head>
<body class="fullcontent">
<div id="quarto-content" class="page-columns page-rows-contents page-layout-article">
<main class="content" id="quarto-document-content">
<section id="deep-dive-tutorial-on-control-structures-in-javascript-with-business-examples" class="level1">
<h1>Deep Dive Tutorial on Control Structures in JavaScript with Business Examples</h1>
<p>Control structures are fundamental components of programming languages that dictate the flow of execution within a program. In JavaScript, control structures enable developers to create dynamic and responsive applications by making decisions, repeating tasks, and handling various conditions. This tutorial provides an in-depth exploration of JavaScript’s control structures, enriched with business-oriented examples to illustrate their practical applications.</p>
<section id="table-of-contents" class="level2">
<h2 class="anchored" data-anchor-id="table-of-contents">Table of Contents</h2>
<ol type="1">
<li><strong>Introduction to Control Structures</strong>
<ul>
<li>What are Control Structures?</li>
<li>Importance of Control Structures in Business Applications</li>
</ul></li>
<li><strong>Conditional Statements</strong>
<ul>
<li><code>if</code> Statement</li>
<li><code>else if</code> and <code>else</code> Statements</li>
<li><code>switch</code> Statement</li>
<li>Ternary Operator</li>
<li>Business Examples:
<ul>
<li>Determining Shipping Costs</li>
<li>Assigning Customer Priority Levels</li>
</ul></li>
</ul></li>
<li><strong>Loops</strong>
<ul>
<li><code>for</code> Loop</li>
<li><code>while</code> Loop</li>
<li><code>do...while</code> Loop</li>
<li><code>for...in</code> Loop</li>
<li><code>for...of</code> Loop</li>
<li><code>forEach</code> Method</li>
<li>Business Examples:
<ul>
<li>Processing Orders</li>
<li>Generating Reports</li>
</ul></li>
</ul></li>
<li><strong>Control Flow Interruptions</strong>
<ul>
<li><code>break</code> Statement</li>
<li><code>continue</code> Statement</li>
<li><code>return</code> Statement in Functions</li>
<li>Business Examples:
<ul>
<li>Validating User Input</li>
<li>Skipping Incomplete Data Entries</li>
</ul></li>
</ul></li>
<li><strong>Logical Operators and Short-Circuit Evaluation</strong>
<ul>
<li>Logical AND (<code>&&</code>) and OR (<code>||</code>)</li>
<li>Short-Circuit Evaluation</li>
<li>Business Examples:
<ul>
<li>Conditional Feature Activation</li>
<li>Efficient Data Validation</li>
</ul></li>
</ul></li>
<li><strong>Nested Control Structures</strong>
<ul>
<li>Concept of Nesting</li>
<li>Best Practices for Nested Structures</li>
<li>Business Examples:
<ul>
<li>Multi-Level Discount Calculations</li>
<li>Complex Inventory Management</li>
</ul></li>
</ul></li>
<li><strong>Conclusion</strong>
<ul>
<li>Recap of Key Concepts</li>
<li>Best Practices in Using Control Structures</li>
<li>Final Thoughts</li>
</ul></li>
</ol>
<hr>
</section>
<section id="introduction-to-control-structures" class="level2">
<h2 class="anchored" data-anchor-id="introduction-to-control-structures">1. Introduction to Control Structures</h2>
<section id="what-are-control-structures" class="level3">
<h3 class="anchored" data-anchor-id="what-are-control-structures">What are Control Structures?</h3>
<p>Control structures are programming constructs that dictate the order in which statements are executed in a program. They allow developers to dictate the flow of logic based on certain conditions or the repetition of tasks. The primary types of control structures in JavaScript include:</p>
<ul>
<li><strong>Conditional Statements</strong>: Execute code blocks based on whether a condition is true or false.</li>
<li><strong>Loops</strong>: Repeat code blocks multiple times.</li>
<li><strong>Control Flow Interruptions</strong>: Alter the flow of execution within loops or functions.</li>
</ul>
</section>
<section id="importance-of-control-structures-in-business-applications" class="level3">
<h3 class="anchored" data-anchor-id="importance-of-control-structures-in-business-applications">Importance of Control Structures in Business Applications</h3>
<p>In business applications, control structures are essential for:</p>
<ul>
<li><strong>Decision Making</strong>: Automating responses based on user inputs or data conditions.</li>
<li><strong>Data Processing</strong>: Iterating over large datasets to perform operations like calculations, validations, or transformations.</li>
<li><strong>Optimizing Performance</strong>: Efficiently managing tasks to enhance the application’s responsiveness and reliability.</li>
</ul>
<hr>
</section>
</section>
<section id="conditional-statements" class="level2">
<h2 class="anchored" data-anchor-id="conditional-statements">2. Conditional Statements</h2>
<p>Conditional statements allow your program to execute certain blocks of code based on whether specified conditions are met.</p>
<section id="if-statement" class="level3">
<h3 class="anchored" data-anchor-id="if-statement"><code>if</code> Statement</h3>
<p>The <code>if</code> statement executes a block of code if a specified condition evaluates to <code>true</code>.</p>
<section id="syntax" class="level4">
<h4 class="anchored" data-anchor-id="syntax">Syntax</h4>
<div class="sourceCode" id="cb1"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> (condition) {</span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a> <span class="co">// Code to execute if condition is true</span></span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</section>
<section id="example" class="level4">
<h4 class="anchored" data-anchor-id="example">Example</h4>
<div class="sourceCode" id="cb2"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="kw">let</span> totalSales <span class="op">=</span> <span class="dv">1500</span><span class="op">;</span></span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> (totalSales <span class="op">></span> <span class="dv">1000</span>) {</span>
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a> <span class="bu">console</span><span class="op">.</span><span class="fu">log</span>(<span class="st">"You have achieved a high sales target!"</span>)<span class="op">;</span></span>
<span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</section>
<section id="output" class="level4">
<h4 class="anchored" data-anchor-id="output">Output</h4>
<pre><code>You have achieved a high sales target!</code></pre>
</section>
</section>
<section id="else-if-and-else-statements" class="level3">
<h3 class="anchored" data-anchor-id="else-if-and-else-statements"><code>else if</code> and <code>else</code> Statements</h3>
<p>The <code>else if</code> and <code>else</code> statements provide additional conditions and a default block if none of the previous conditions are met.</p>
<section id="syntax-1" class="level4">
<h4 class="anchored" data-anchor-id="syntax-1">Syntax</h4>
<div class="sourceCode" id="cb4"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> (condition1) {</span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a> <span class="co">// Code if condition1 is true</span></span>
<span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a>} <span class="cf">else</span> <span class="cf">if</span> (condition2) {</span>
<span id="cb4-4"><a href="#cb4-4" aria-hidden="true" tabindex="-1"></a> <span class="co">// Code if condition2 is true</span></span>
<span id="cb4-5"><a href="#cb4-5" aria-hidden="true" tabindex="-1"></a>} <span class="cf">else</span> {</span>
<span id="cb4-6"><a href="#cb4-6" aria-hidden="true" tabindex="-1"></a> <span class="co">// Code if none of the above conditions are true</span></span>
<span id="cb4-7"><a href="#cb4-7" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</section>
<section id="example-1" class="level4">
<h4 class="anchored" data-anchor-id="example-1">Example</h4>
<div class="sourceCode" id="cb5"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="kw">let</span> customerType <span class="op">=</span> <span class="st">"premium"</span><span class="op">;</span></span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> (customerType <span class="op">===</span> <span class="st">"new"</span>) {</span>
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a> <span class="bu">console</span><span class="op">.</span><span class="fu">log</span>(<span class="st">"Offer a welcome discount."</span>)<span class="op">;</span></span>
<span id="cb5-5"><a href="#cb5-5" aria-hidden="true" tabindex="-1"></a>} <span class="cf">else</span> <span class="cf">if</span> (customerType <span class="op">===</span> <span class="st">"premium"</span>) {</span>
<span id="cb5-6"><a href="#cb5-6" aria-hidden="true" tabindex="-1"></a> <span class="bu">console</span><span class="op">.</span><span class="fu">log</span>(<span class="st">"Provide premium support."</span>)<span class="op">;</span></span>
<span id="cb5-7"><a href="#cb5-7" aria-hidden="true" tabindex="-1"></a>} <span class="cf">else</span> {</span>
<span id="cb5-8"><a href="#cb5-8" aria-hidden="true" tabindex="-1"></a> <span class="bu">console</span><span class="op">.</span><span class="fu">log</span>(<span class="st">"Standard service package."</span>)<span class="op">;</span></span>
<span id="cb5-9"><a href="#cb5-9" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</section>
<section id="output-1" class="level4">
<h4 class="anchored" data-anchor-id="output-1">Output</h4>
<pre><code>Provide premium support.</code></pre>
</section>
</section>
<section id="switch-statement" class="level3">
<h3 class="anchored" data-anchor-id="switch-statement"><code>switch</code> Statement</h3>
<p>The <code>switch</code> statement evaluates an expression and executes code blocks based on matching case values.</p>
<section id="syntax-2" class="level4">
<h4 class="anchored" data-anchor-id="syntax-2">Syntax</h4>
<div class="sourceCode" id="cb7"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="cf">switch</span> (expression) {</span>
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a> <span class="cf">case</span> <span class="dt">value1</span><span class="op">:</span></span>
<span id="cb7-3"><a href="#cb7-3" aria-hidden="true" tabindex="-1"></a> <span class="co">// Code to execute for value1</span></span>
<span id="cb7-4"><a href="#cb7-4" aria-hidden="true" tabindex="-1"></a> <span class="cf">break</span><span class="op">;</span></span>
<span id="cb7-5"><a href="#cb7-5" aria-hidden="true" tabindex="-1"></a> <span class="cf">case</span> <span class="dt">value2</span><span class="op">:</span></span>
<span id="cb7-6"><a href="#cb7-6" aria-hidden="true" tabindex="-1"></a> <span class="co">// Code to execute for value2</span></span>
<span id="cb7-7"><a href="#cb7-7" aria-hidden="true" tabindex="-1"></a> <span class="cf">break</span><span class="op">;</span></span>
<span id="cb7-8"><a href="#cb7-8" aria-hidden="true" tabindex="-1"></a> <span class="cf">default</span><span class="op">:</span></span>
<span id="cb7-9"><a href="#cb7-9" aria-hidden="true" tabindex="-1"></a> <span class="co">// Code to execute if no cases match</span></span>
<span id="cb7-10"><a href="#cb7-10" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</section>
<section id="example-2" class="level4">
<h4 class="anchored" data-anchor-id="example-2">Example</h4>
<div class="sourceCode" id="cb8"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="kw">let</span> paymentMethod <span class="op">=</span> <span class="st">"Credit Card"</span><span class="op">;</span></span>
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-3"><a href="#cb8-3" aria-hidden="true" tabindex="-1"></a><span class="cf">switch</span> (paymentMethod) {</span>
<span id="cb8-4"><a href="#cb8-4" aria-hidden="true" tabindex="-1"></a> <span class="cf">case</span> <span class="st">"Credit Card"</span><span class="op">:</span></span>
<span id="cb8-5"><a href="#cb8-5" aria-hidden="true" tabindex="-1"></a> <span class="bu">console</span><span class="op">.</span><span class="fu">log</span>(<span class="st">"Processing credit card payment."</span>)<span class="op">;</span></span>
<span id="cb8-6"><a href="#cb8-6" aria-hidden="true" tabindex="-1"></a> <span class="cf">break</span><span class="op">;</span></span>
<span id="cb8-7"><a href="#cb8-7" aria-hidden="true" tabindex="-1"></a> <span class="cf">case</span> <span class="st">"PayPal"</span><span class="op">:</span></span>
<span id="cb8-8"><a href="#cb8-8" aria-hidden="true" tabindex="-1"></a> <span class="bu">console</span><span class="op">.</span><span class="fu">log</span>(<span class="st">"Processing PayPal payment."</span>)<span class="op">;</span></span>
<span id="cb8-9"><a href="#cb8-9" aria-hidden="true" tabindex="-1"></a> <span class="cf">break</span><span class="op">;</span></span>
<span id="cb8-10"><a href="#cb8-10" aria-hidden="true" tabindex="-1"></a> <span class="cf">case</span> <span class="st">"Bank Transfer"</span><span class="op">:</span></span>
<span id="cb8-11"><a href="#cb8-11" aria-hidden="true" tabindex="-1"></a> <span class="bu">console</span><span class="op">.</span><span class="fu">log</span>(<span class="st">"Processing bank transfer."</span>)<span class="op">;</span></span>
<span id="cb8-12"><a href="#cb8-12" aria-hidden="true" tabindex="-1"></a> <span class="cf">break</span><span class="op">;</span></span>
<span id="cb8-13"><a href="#cb8-13" aria-hidden="true" tabindex="-1"></a> <span class="cf">default</span><span class="op">:</span></span>
<span id="cb8-14"><a href="#cb8-14" aria-hidden="true" tabindex="-1"></a> <span class="bu">console</span><span class="op">.</span><span class="fu">log</span>(<span class="st">"Unsupported payment method."</span>)<span class="op">;</span></span>
<span id="cb8-15"><a href="#cb8-15" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</section>
<section id="output-2" class="level4">
<h4 class="anchored" data-anchor-id="output-2">Output</h4>
<pre><code>Processing credit card payment.</code></pre>
</section>
</section>
<section id="ternary-operator" class="level3">
<h3 class="anchored" data-anchor-id="ternary-operator">Ternary Operator</h3>
<p>The ternary operator provides a shorthand way of writing <code>if...else</code> statements.</p>
<section id="syntax-3" class="level4">
<h4 class="anchored" data-anchor-id="syntax-3">Syntax</h4>
<div class="sourceCode" id="cb10"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a>condition <span class="op">?</span> expressionIfTrue <span class="op">:</span> expressionIfFalse<span class="op">;</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</section>
<section id="example-3" class="level4">
<h4 class="anchored" data-anchor-id="example-3">Example</h4>
<div class="sourceCode" id="cb11"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a><span class="kw">let</span> stock <span class="op">=</span> <span class="dv">20</span><span class="op">;</span></span>
<span id="cb11-2"><a href="#cb11-2" aria-hidden="true" tabindex="-1"></a><span class="kw">let</span> message <span class="op">=</span> stock <span class="op">></span> <span class="dv">0</span> <span class="op">?</span> <span class="st">"In Stock"</span> <span class="op">:</span> <span class="st">"Out of Stock"</span><span class="op">;</span></span>
<span id="cb11-3"><a href="#cb11-3" aria-hidden="true" tabindex="-1"></a><span class="bu">console</span><span class="op">.</span><span class="fu">log</span>(message)<span class="op">;</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</section>
<section id="output-3" class="level4">
<h4 class="anchored" data-anchor-id="output-3">Output</h4>
<pre><code>In Stock</code></pre>
</section>
</section>
<section id="business-examples" class="level3">
<h3 class="anchored" data-anchor-id="business-examples">Business Examples</h3>
<section id="business-example-1-determining-shipping-costs" class="level4">
<h4 class="anchored" data-anchor-id="business-example-1-determining-shipping-costs">Business Example 1: Determining Shipping Costs</h4>
<p>Calculate shipping costs based on the total order amount.</p>
<div class="sourceCode" id="cb13"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a><span class="kw">function</span> <span class="fu">calculateShipping</span>(totalAmount) {</span>
<span id="cb13-2"><a href="#cb13-2" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (totalAmount <span class="op">></span> <span class="dv">500</span>) {</span>
<span id="cb13-3"><a href="#cb13-3" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> <span class="st">"Free Shipping"</span><span class="op">;</span></span>
<span id="cb13-4"><a href="#cb13-4" aria-hidden="true" tabindex="-1"></a> } <span class="cf">else</span> <span class="cf">if</span> (totalAmount <span class="op">></span> <span class="dv">200</span>) {</span>
<span id="cb13-5"><a href="#cb13-5" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> <span class="st">"$10 Shipping Fee"</span><span class="op">;</span></span>
<span id="cb13-6"><a href="#cb13-6" aria-hidden="true" tabindex="-1"></a> } <span class="cf">else</span> {</span>
<span id="cb13-7"><a href="#cb13-7" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> <span class="st">"$20 Shipping Fee"</span><span class="op">;</span></span>
<span id="cb13-8"><a href="#cb13-8" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb13-9"><a href="#cb13-9" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb13-10"><a href="#cb13-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb13-11"><a href="#cb13-11" aria-hidden="true" tabindex="-1"></a><span class="kw">let</span> orderAmount <span class="op">=</span> <span class="dv">250</span><span class="op">;</span></span>
<span id="cb13-12"><a href="#cb13-12" aria-hidden="true" tabindex="-1"></a><span class="kw">let</span> shippingCost <span class="op">=</span> <span class="fu">calculateShipping</span>(orderAmount)<span class="op">;</span></span>
<span id="cb13-13"><a href="#cb13-13" aria-hidden="true" tabindex="-1"></a><span class="bu">console</span><span class="op">.</span><span class="fu">log</span>(<span class="vs">`Shipping Cost: </span><span class="sc">${</span>shippingCost<span class="sc">}</span><span class="vs">`</span>)<span class="op">;</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<p><strong>Output:</strong></p>
<pre><code>Shipping Cost: $10 Shipping Fee</code></pre>
<p><strong>Explanation:</strong></p>
<ul>
<li>Orders above $500 receive free shipping.</li>
<li>Orders between $201 and $500 are charged a $10 fee.</li>
<li>Orders $200 or below incur a $20 shipping fee.</li>
</ul>
</section>
<section id="business-example-2-assigning-customer-priority-levels" class="level4">
<h4 class="anchored" data-anchor-id="business-example-2-assigning-customer-priority-levels">Business Example 2: Assigning Customer Priority Levels</h4>
<p>Assign priority levels to customers based on their purchase history.</p>
<div class="sourceCode" id="cb15"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a><span class="kw">function</span> <span class="fu">getCustomerPriority</span>(totalPurchases) {</span>
<span id="cb15-2"><a href="#cb15-2" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (totalPurchases <span class="op">></span> <span class="dv">10000</span>) {</span>
<span id="cb15-3"><a href="#cb15-3" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> <span class="st">"Platinum"</span><span class="op">;</span></span>
<span id="cb15-4"><a href="#cb15-4" aria-hidden="true" tabindex="-1"></a> } <span class="cf">else</span> <span class="cf">if</span> (totalPurchases <span class="op">></span> <span class="dv">5000</span>) {</span>
<span id="cb15-5"><a href="#cb15-5" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> <span class="st">"Gold"</span><span class="op">;</span></span>
<span id="cb15-6"><a href="#cb15-6" aria-hidden="true" tabindex="-1"></a> } <span class="cf">else</span> <span class="cf">if</span> (totalPurchases <span class="op">></span> <span class="dv">1000</span>) {</span>
<span id="cb15-7"><a href="#cb15-7" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> <span class="st">"Silver"</span><span class="op">;</span></span>
<span id="cb15-8"><a href="#cb15-8" aria-hidden="true" tabindex="-1"></a> } <span class="cf">else</span> {</span>
<span id="cb15-9"><a href="#cb15-9" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> <span class="st">"Bronze"</span><span class="op">;</span></span>
<span id="cb15-10"><a href="#cb15-10" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb15-11"><a href="#cb15-11" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb15-12"><a href="#cb15-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb15-13"><a href="#cb15-13" aria-hidden="true" tabindex="-1"></a><span class="kw">let</span> customerPurchases <span class="op">=</span> <span class="dv">7500</span><span class="op">;</span></span>
<span id="cb15-14"><a href="#cb15-14" aria-hidden="true" tabindex="-1"></a><span class="kw">let</span> priorityLevel <span class="op">=</span> <span class="fu">getCustomerPriority</span>(customerPurchases)<span class="op">;</span></span>
<span id="cb15-15"><a href="#cb15-15" aria-hidden="true" tabindex="-1"></a><span class="bu">console</span><span class="op">.</span><span class="fu">log</span>(<span class="vs">`Customer Priority Level: </span><span class="sc">${</span>priorityLevel<span class="sc">}</span><span class="vs">`</span>)<span class="op">;</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<p><strong>Output:</strong></p>
<pre><code>Customer Priority Level: Gold</code></pre>
<p><strong>Explanation:</strong></p>
<ul>
<li><strong>Platinum</strong>: Customers with purchases over $10,000.</li>
<li><strong>Gold</strong>: Customers with purchases between $5,001 and $10,000.</li>
<li><strong>Silver</strong>: Customers with purchases between $1,001 and $5,000.</li>
<li><strong>Bronze</strong>: Customers with purchases up to $1,000.</li>
</ul>
<hr>
</section>
</section>
</section>
<section id="loops" class="level2">
<h2 class="anchored" data-anchor-id="loops">3. Loops</h2>
<p>Loops allow you to execute a block of code multiple times, which is particularly useful for processing collections of data.</p>
<section id="for-loop" class="level3">
<h3 class="anchored" data-anchor-id="for-loop"><code>for</code> Loop</h3>
<p>The <code>for</code> loop is used when the number of iterations is known beforehand.</p>
<section id="syntax-4" class="level4">
<h4 class="anchored" data-anchor-id="syntax-4">Syntax</h4>
<div class="sourceCode" id="cb17"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb17-1"><a href="#cb17-1" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> (initialization<span class="op">;</span> condition<span class="op">;</span> increment) {</span>
<span id="cb17-2"><a href="#cb17-2" aria-hidden="true" tabindex="-1"></a> <span class="co">// Code to execute in each iteration</span></span>
<span id="cb17-3"><a href="#cb17-3" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</section>
<section id="example-4" class="level4">
<h4 class="anchored" data-anchor-id="example-4">Example</h4>
<div class="sourceCode" id="cb18"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> (<span class="kw">let</span> i <span class="op">=</span> <span class="dv">1</span><span class="op">;</span> i <span class="op"><=</span> <span class="dv">5</span><span class="op">;</span> i<span class="op">++</span>) {</span>
<span id="cb18-2"><a href="#cb18-2" aria-hidden="true" tabindex="-1"></a> <span class="bu">console</span><span class="op">.</span><span class="fu">log</span>(<span class="vs">`Order Number: </span><span class="sc">${</span>i<span class="sc">}</span><span class="vs">`</span>)<span class="op">;</span></span>
<span id="cb18-3"><a href="#cb18-3" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</section>
<section id="output-4" class="level4">
<h4 class="anchored" data-anchor-id="output-4">Output</h4>
<pre><code>Order Number: 1
Order Number: 2
Order Number: 3
Order Number: 4
Order Number: 5</code></pre>
</section>
</section>
<section id="while-loop" class="level3">
<h3 class="anchored" data-anchor-id="while-loop"><code>while</code> Loop</h3>
<p>The <code>while</code> loop continues to execute as long as the specified condition remains <code>true</code>.</p>
<section id="syntax-5" class="level4">
<h4 class="anchored" data-anchor-id="syntax-5">Syntax</h4>
<div class="sourceCode" id="cb20"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a><span class="cf">while</span> (condition) {</span>
<span id="cb20-2"><a href="#cb20-2" aria-hidden="true" tabindex="-1"></a> <span class="co">// Code to execute</span></span>
<span id="cb20-3"><a href="#cb20-3" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</section>
<section id="example-5" class="level4">
<h4 class="anchored" data-anchor-id="example-5">Example</h4>
<div class="sourceCode" id="cb21"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb21-1"><a href="#cb21-1" aria-hidden="true" tabindex="-1"></a><span class="kw">let</span> count <span class="op">=</span> <span class="dv">1</span><span class="op">;</span></span>
<span id="cb21-2"><a href="#cb21-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb21-3"><a href="#cb21-3" aria-hidden="true" tabindex="-1"></a><span class="cf">while</span> (count <span class="op"><=</span> <span class="dv">3</span>) {</span>
<span id="cb21-4"><a href="#cb21-4" aria-hidden="true" tabindex="-1"></a> <span class="bu">console</span><span class="op">.</span><span class="fu">log</span>(<span class="vs">`Processing item </span><span class="sc">${</span>count<span class="sc">}</span><span class="vs">`</span>)<span class="op">;</span></span>
<span id="cb21-5"><a href="#cb21-5" aria-hidden="true" tabindex="-1"></a> count<span class="op">++;</span></span>
<span id="cb21-6"><a href="#cb21-6" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</section>
<section id="output-5" class="level4">
<h4 class="anchored" data-anchor-id="output-5">Output</h4>
<pre><code>Processing item 1
Processing item 2
Processing item 3</code></pre>
</section>
</section>
<section id="do...while-loop" class="level3">
<h3 class="anchored" data-anchor-id="do...while-loop"><code>do...while</code> Loop</h3>
<p>The <code>do...while</code> loop executes the code block once before checking the condition.</p>
<section id="syntax-6" class="level4">
<h4 class="anchored" data-anchor-id="syntax-6">Syntax</h4>
<div class="sourceCode" id="cb23"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb23-1"><a href="#cb23-1" aria-hidden="true" tabindex="-1"></a><span class="cf">do</span> {</span>
<span id="cb23-2"><a href="#cb23-2" aria-hidden="true" tabindex="-1"></a> <span class="co">// Code to execute</span></span>
<span id="cb23-3"><a href="#cb23-3" aria-hidden="true" tabindex="-1"></a>} <span class="cf">while</span> (condition)<span class="op">;</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</section>
<section id="example-6" class="level4">
<h4 class="anchored" data-anchor-id="example-6">Example</h4>
<div class="sourceCode" id="cb24"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb24-1"><a href="#cb24-1" aria-hidden="true" tabindex="-1"></a><span class="kw">let</span> attempt <span class="op">=</span> <span class="dv">1</span><span class="op">;</span></span>
<span id="cb24-2"><a href="#cb24-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb24-3"><a href="#cb24-3" aria-hidden="true" tabindex="-1"></a><span class="cf">do</span> {</span>
<span id="cb24-4"><a href="#cb24-4" aria-hidden="true" tabindex="-1"></a> <span class="bu">console</span><span class="op">.</span><span class="fu">log</span>(<span class="vs">`Login attempt </span><span class="sc">${</span>attempt<span class="sc">}</span><span class="vs">`</span>)<span class="op">;</span></span>
<span id="cb24-5"><a href="#cb24-5" aria-hidden="true" tabindex="-1"></a> attempt<span class="op">++;</span></span>
<span id="cb24-6"><a href="#cb24-6" aria-hidden="true" tabindex="-1"></a>} <span class="cf">while</span> (attempt <span class="op"><=</span> <span class="dv">2</span>)<span class="op">;</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</section>
<section id="output-6" class="level4">
<h4 class="anchored" data-anchor-id="output-6">Output</h4>
<pre><code>Login attempt 1
Login attempt 2</code></pre>
</section>
</section>
<section id="for...in-loop" class="level3">
<h3 class="anchored" data-anchor-id="for...in-loop"><code>for...in</code> Loop</h3>
<p>The <code>for...in</code> loop iterates over the enumerable properties of an object.</p>
<section id="syntax-7" class="level4">
<h4 class="anchored" data-anchor-id="syntax-7">Syntax</h4>
<div class="sourceCode" id="cb26"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb26-1"><a href="#cb26-1" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> (<span class="kw">let</span> key <span class="kw">in</span> object) {</span>
<span id="cb26-2"><a href="#cb26-2" aria-hidden="true" tabindex="-1"></a> <span class="co">// Code to execute</span></span>
<span id="cb26-3"><a href="#cb26-3" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</section>
<section id="example-7" class="level4">
<h4 class="anchored" data-anchor-id="example-7">Example</h4>
<div class="sourceCode" id="cb27"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb27-1"><a href="#cb27-1" aria-hidden="true" tabindex="-1"></a><span class="kw">let</span> product <span class="op">=</span> {</span>
<span id="cb27-2"><a href="#cb27-2" aria-hidden="true" tabindex="-1"></a> <span class="dt">name</span><span class="op">:</span> <span class="st">"Laptop"</span><span class="op">,</span></span>
<span id="cb27-3"><a href="#cb27-3" aria-hidden="true" tabindex="-1"></a> <span class="dt">price</span><span class="op">:</span> <span class="dv">1200</span><span class="op">,</span></span>
<span id="cb27-4"><a href="#cb27-4" aria-hidden="true" tabindex="-1"></a> <span class="dt">stock</span><span class="op">:</span> <span class="dv">30</span></span>
<span id="cb27-5"><a href="#cb27-5" aria-hidden="true" tabindex="-1"></a>}<span class="op">;</span></span>
<span id="cb27-6"><a href="#cb27-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb27-7"><a href="#cb27-7" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> (<span class="kw">let</span> property <span class="kw">in</span> product) {</span>
<span id="cb27-8"><a href="#cb27-8" aria-hidden="true" tabindex="-1"></a> <span class="bu">console</span><span class="op">.</span><span class="fu">log</span>(<span class="vs">`</span><span class="sc">${</span>property<span class="sc">}</span><span class="vs">: </span><span class="sc">${</span>product[property]<span class="sc">}</span><span class="vs">`</span>)<span class="op">;</span></span>
<span id="cb27-9"><a href="#cb27-9" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</section>
<section id="output-7" class="level4">
<h4 class="anchored" data-anchor-id="output-7">Output</h4>
<pre><code>name: Laptop
price: 1200
stock: 30</code></pre>
</section>
</section>
<section id="for...of-loop" class="level3">
<h3 class="anchored" data-anchor-id="for...of-loop"><code>for...of</code> Loop</h3>
<p>The `for…of</p>
<p>` loop iterates over iterable objects like arrays, strings, etc.</p>
<section id="syntax-8" class="level4">
<h4 class="anchored" data-anchor-id="syntax-8">Syntax</h4>
<div class="sourceCode" id="cb29"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb29-1"><a href="#cb29-1" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> (<span class="kw">let</span> element <span class="kw">of</span> iterable) {</span>
<span id="cb29-2"><a href="#cb29-2" aria-hidden="true" tabindex="-1"></a> <span class="co">// Code to execute</span></span>
<span id="cb29-3"><a href="#cb29-3" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</section>
<section id="example-8" class="level4">
<h4 class="anchored" data-anchor-id="example-8">Example</h4>
<div class="sourceCode" id="cb30"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb30-1"><a href="#cb30-1" aria-hidden="true" tabindex="-1"></a><span class="kw">let</span> customers <span class="op">=</span> [<span class="st">"Alice"</span><span class="op">,</span> <span class="st">"Bob"</span><span class="op">,</span> <span class="st">"Charlie"</span>]<span class="op">;</span></span>
<span id="cb30-2"><a href="#cb30-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb30-3"><a href="#cb30-3" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> (<span class="kw">let</span> customer <span class="kw">of</span> customers) {</span>
<span id="cb30-4"><a href="#cb30-4" aria-hidden="true" tabindex="-1"></a> <span class="bu">console</span><span class="op">.</span><span class="fu">log</span>(<span class="vs">`Welcome, </span><span class="sc">${</span>customer<span class="sc">}</span><span class="vs">!`</span>)<span class="op">;</span></span>
<span id="cb30-5"><a href="#cb30-5" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</section>
<section id="output-8" class="level4">
<h4 class="anchored" data-anchor-id="output-8">Output</h4>
<pre><code>Welcome, Alice!
Welcome, Bob!
Welcome, Charlie!</code></pre>
</section>
</section>
<section id="foreach-method" class="level3">
<h3 class="anchored" data-anchor-id="foreach-method"><code>forEach</code> Method</h3>
<p>The <code>forEach</code> method executes a provided function once for each array element.</p>
<section id="syntax-9" class="level4">
<h4 class="anchored" data-anchor-id="syntax-9">Syntax</h4>
<div class="sourceCode" id="cb32"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb32-1"><a href="#cb32-1" aria-hidden="true" tabindex="-1"></a>array<span class="op">.</span><span class="fu">forEach</span>(<span class="kw">function</span>(element<span class="op">,</span> index<span class="op">,</span> array) {</span>
<span id="cb32-2"><a href="#cb32-2" aria-hidden="true" tabindex="-1"></a> <span class="co">// Code to execute</span></span>
<span id="cb32-3"><a href="#cb32-3" aria-hidden="true" tabindex="-1"></a>})<span class="op">;</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</section>
<section id="example-9" class="level4">
<h4 class="anchored" data-anchor-id="example-9">Example</h4>
<div class="sourceCode" id="cb33"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb33-1"><a href="#cb33-1" aria-hidden="true" tabindex="-1"></a><span class="kw">let</span> sales <span class="op">=</span> [<span class="dv">500</span><span class="op">,</span> <span class="dv">750</span><span class="op">,</span> <span class="dv">1200</span>]<span class="op">;</span></span>
<span id="cb33-2"><a href="#cb33-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-3"><a href="#cb33-3" aria-hidden="true" tabindex="-1"></a>sales<span class="op">.</span><span class="fu">forEach</span>((sale<span class="op">,</span> index) <span class="kw">=></span> {</span>
<span id="cb33-4"><a href="#cb33-4" aria-hidden="true" tabindex="-1"></a> <span class="bu">console</span><span class="op">.</span><span class="fu">log</span>(<span class="vs">`Sale </span><span class="sc">${</span>index <span class="op">+</span> <span class="dv">1</span><span class="sc">}</span><span class="vs">: $</span><span class="sc">${</span>sale<span class="sc">}</span><span class="vs">`</span>)<span class="op">;</span></span>
<span id="cb33-5"><a href="#cb33-5" aria-hidden="true" tabindex="-1"></a>})<span class="op">;</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</section>
<section id="output-9" class="level4">
<h4 class="anchored" data-anchor-id="output-9">Output</h4>
<pre><code>Sale 1: $500
Sale 2: $750
Sale 3: $1200</code></pre>
</section>
</section>
<section id="business-examples-1" class="level3">
<h3 class="anchored" data-anchor-id="business-examples-1">Business Examples</h3>
<section id="business-example-1-processing-orders" class="level4">
<h4 class="anchored" data-anchor-id="business-example-1-processing-orders">Business Example 1: Processing Orders</h4>
<p>Iterate through a list of orders to calculate the total revenue.</p>
<div class="sourceCode" id="cb35"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb35-1"><a href="#cb35-1" aria-hidden="true" tabindex="-1"></a><span class="kw">let</span> orders <span class="op">=</span> [</span>
<span id="cb35-2"><a href="#cb35-2" aria-hidden="true" tabindex="-1"></a> { <span class="dt">id</span><span class="op">:</span> <span class="dv">1</span><span class="op">,</span> <span class="dt">amount</span><span class="op">:</span> <span class="dv">250</span> }<span class="op">,</span></span>
<span id="cb35-3"><a href="#cb35-3" aria-hidden="true" tabindex="-1"></a> { <span class="dt">id</span><span class="op">:</span> <span class="dv">2</span><span class="op">,</span> <span class="dt">amount</span><span class="op">:</span> <span class="dv">450</span> }<span class="op">,</span></span>
<span id="cb35-4"><a href="#cb35-4" aria-hidden="true" tabindex="-1"></a> { <span class="dt">id</span><span class="op">:</span> <span class="dv">3</span><span class="op">,</span> <span class="dt">amount</span><span class="op">:</span> <span class="dv">300</span> }</span>
<span id="cb35-5"><a href="#cb35-5" aria-hidden="true" tabindex="-1"></a>]<span class="op">;</span></span>
<span id="cb35-6"><a href="#cb35-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb35-7"><a href="#cb35-7" aria-hidden="true" tabindex="-1"></a><span class="kw">let</span> totalRevenue <span class="op">=</span> <span class="dv">0</span><span class="op">;</span></span>
<span id="cb35-8"><a href="#cb35-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb35-9"><a href="#cb35-9" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> (<span class="kw">let</span> i <span class="op">=</span> <span class="dv">0</span><span class="op">;</span> i <span class="op"><</span> orders<span class="op">.</span><span class="at">length</span><span class="op">;</span> i<span class="op">++</span>) {</span>
<span id="cb35-10"><a href="#cb35-10" aria-hidden="true" tabindex="-1"></a> totalRevenue <span class="op">+=</span> orders[i]<span class="op">.</span><span class="at">amount</span><span class="op">;</span></span>
<span id="cb35-11"><a href="#cb35-11" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb35-12"><a href="#cb35-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb35-13"><a href="#cb35-13" aria-hidden="true" tabindex="-1"></a><span class="bu">console</span><span class="op">.</span><span class="fu">log</span>(<span class="vs">`Total Revenue: $</span><span class="sc">${</span>totalRevenue<span class="sc">}</span><span class="vs">`</span>)<span class="op">;</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<p><strong>Output:</strong></p>
<pre><code>Total Revenue: $1000</code></pre>
<p><strong>Explanation:</strong></p>
<ul>
<li>The <code>for</code> loop iterates over each order in the <code>orders</code> array, summing up the <code>amount</code> to calculate total revenue.</li>
</ul>
</section>
<section id="business-example-2-generating-reports" class="level4">
<h4 class="anchored" data-anchor-id="business-example-2-generating-reports">Business Example 2: Generating Reports</h4>
<p>Use a <code>forEach</code> loop to generate a sales report.</p>
<div class="sourceCode" id="cb37"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb37-1"><a href="#cb37-1" aria-hidden="true" tabindex="-1"></a><span class="kw">let</span> salesData <span class="op">=</span> [</span>
<span id="cb37-2"><a href="#cb37-2" aria-hidden="true" tabindex="-1"></a> { <span class="dt">product</span><span class="op">:</span> <span class="st">"Laptop"</span><span class="op">,</span> <span class="dt">unitsSold</span><span class="op">:</span> <span class="dv">50</span> }<span class="op">,</span></span>
<span id="cb37-3"><a href="#cb37-3" aria-hidden="true" tabindex="-1"></a> { <span class="dt">product</span><span class="op">:</span> <span class="st">"Smartphone"</span><span class="op">,</span> <span class="dt">unitsSold</span><span class="op">:</span> <span class="dv">150</span> }<span class="op">,</span></span>
<span id="cb37-4"><a href="#cb37-4" aria-hidden="true" tabindex="-1"></a> { <span class="dt">product</span><span class="op">:</span> <span class="st">"Tablet"</span><span class="op">,</span> <span class="dt">unitsSold</span><span class="op">:</span> <span class="dv">80</span> }</span>
<span id="cb37-5"><a href="#cb37-5" aria-hidden="true" tabindex="-1"></a>]<span class="op">;</span></span>
<span id="cb37-6"><a href="#cb37-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb37-7"><a href="#cb37-7" aria-hidden="true" tabindex="-1"></a><span class="bu">console</span><span class="op">.</span><span class="fu">log</span>(<span class="st">"Sales Report:"</span>)<span class="op">;</span></span>
<span id="cb37-8"><a href="#cb37-8" aria-hidden="true" tabindex="-1"></a>salesData<span class="op">.</span><span class="fu">forEach</span>((item) <span class="kw">=></span> {</span>
<span id="cb37-9"><a href="#cb37-9" aria-hidden="true" tabindex="-1"></a> <span class="bu">console</span><span class="op">.</span><span class="fu">log</span>(<span class="vs">`</span><span class="sc">${</span>item<span class="op">.</span><span class="at">product</span><span class="sc">}</span><span class="vs">: </span><span class="sc">${</span>item<span class="op">.</span><span class="at">unitsSold</span><span class="sc">}</span><span class="vs"> units sold`</span>)<span class="op">;</span></span>
<span id="cb37-10"><a href="#cb37-10" aria-hidden="true" tabindex="-1"></a>})<span class="op">;</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<p><strong>Output:</strong></p>
<pre><code>Sales Report:
Laptop: 50 units sold
Smartphone: 150 units sold
Tablet: 80 units sold</code></pre>
<p><strong>Explanation:</strong></p>
<ul>
<li>The <code>forEach</code> method iterates over each item in <code>salesData</code>, printing out the product name and units sold.</li>
</ul>
<hr>
</section>
</section>
</section>
<section id="control-flow-interruptions" class="level2">
<h2 class="anchored" data-anchor-id="control-flow-interruptions">4. Control Flow Interruptions</h2>
<p>Control flow interruptions allow you to alter the normal flow of execution within loops or functions.</p>
<section id="break-statement" class="level3">
<h3 class="anchored" data-anchor-id="break-statement"><code>break</code> Statement</h3>
<p>The <code>break</code> statement terminates the current loop or switch statement.</p>
<section id="syntax-10" class="level4">
<h4 class="anchored" data-anchor-id="syntax-10">Syntax</h4>
<div class="sourceCode" id="cb39"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb39-1"><a href="#cb39-1" aria-hidden="true" tabindex="-1"></a><span class="cf">break</span><span class="op">;</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</section>
<section id="example-10" class="level4">
<h4 class="anchored" data-anchor-id="example-10">Example</h4>
<div class="sourceCode" id="cb40"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb40-1"><a href="#cb40-1" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> (<span class="kw">let</span> i <span class="op">=</span> <span class="dv">1</span><span class="op">;</span> i <span class="op"><=</span> <span class="dv">10</span><span class="op">;</span> i<span class="op">++</span>) {</span>
<span id="cb40-2"><a href="#cb40-2" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (i <span class="op">===</span> <span class="dv">5</span>) {</span>
<span id="cb40-3"><a href="#cb40-3" aria-hidden="true" tabindex="-1"></a> <span class="cf">break</span><span class="op">;</span> <span class="co">// Exit the loop when i is 5</span></span>
<span id="cb40-4"><a href="#cb40-4" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb40-5"><a href="#cb40-5" aria-hidden="true" tabindex="-1"></a> <span class="bu">console</span><span class="op">.</span><span class="fu">log</span>(<span class="vs">`Number: </span><span class="sc">${</span>i<span class="sc">}</span><span class="vs">`</span>)<span class="op">;</span></span>
<span id="cb40-6"><a href="#cb40-6" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</section>
<section id="output-10" class="level4">
<h4 class="anchored" data-anchor-id="output-10">Output</h4>
<pre><code>Number: 1
Number: 2
Number: 3
Number: 4</code></pre>
</section>
</section>
<section id="continue-statement" class="level3">
<h3 class="anchored" data-anchor-id="continue-statement"><code>continue</code> Statement</h3>
<p>The <code>continue</code> statement skips the current iteration and moves to the next one.</p>
<section id="syntax-11" class="level4">
<h4 class="anchored" data-anchor-id="syntax-11">Syntax</h4>
<div class="sourceCode" id="cb42"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb42-1"><a href="#cb42-1" aria-hidden="true" tabindex="-1"></a><span class="cf">continue</span><span class="op">;</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</section>
<section id="example-11" class="level4">
<h4 class="anchored" data-anchor-id="example-11">Example</h4>
<div class="sourceCode" id="cb43"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb43-1"><a href="#cb43-1" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> (<span class="kw">let</span> i <span class="op">=</span> <span class="dv">1</span><span class="op">;</span> i <span class="op"><=</span> <span class="dv">5</span><span class="op">;</span> i<span class="op">++</span>) {</span>
<span id="cb43-2"><a href="#cb43-2" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (i <span class="op">===</span> <span class="dv">3</span>) {</span>
<span id="cb43-3"><a href="#cb43-3" aria-hidden="true" tabindex="-1"></a> <span class="cf">continue</span><span class="op">;</span> <span class="co">// Skip when i is 3</span></span>
<span id="cb43-4"><a href="#cb43-4" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb43-5"><a href="#cb43-5" aria-hidden="true" tabindex="-1"></a> <span class="bu">console</span><span class="op">.</span><span class="fu">log</span>(<span class="vs">`Iteration: </span><span class="sc">${</span>i<span class="sc">}</span><span class="vs">`</span>)<span class="op">;</span></span>
<span id="cb43-6"><a href="#cb43-6" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</section>
<section id="output-11" class="level4">
<h4 class="anchored" data-anchor-id="output-11">Output</h4>
<pre><code>Iteration: 1
Iteration: 2
Iteration: 4
Iteration: 5</code></pre>
</section>
</section>
<section id="return-statement-in-functions" class="level3">
<h3 class="anchored" data-anchor-id="return-statement-in-functions"><code>return</code> Statement in Functions</h3>
<p>The <code>return</code> statement exits a function and optionally returns a value.</p>
<section id="syntax-12" class="level4">
<h4 class="anchored" data-anchor-id="syntax-12">Syntax</h4>
<div class="sourceCode" id="cb45"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb45-1"><a href="#cb45-1" aria-hidden="true" tabindex="-1"></a><span class="cf">return</span> value<span class="op">;</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</section>
<section id="example-12" class="level4">
<h4 class="anchored" data-anchor-id="example-12">Example</h4>
<div class="sourceCode" id="cb46"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb46-1"><a href="#cb46-1" aria-hidden="true" tabindex="-1"></a><span class="kw">function</span> <span class="fu">findFirstLargeSale</span>(sales) {</span>
<span id="cb46-2"><a href="#cb46-2" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span> (<span class="kw">let</span> sale <span class="kw">of</span> sales) {</span>
<span id="cb46-3"><a href="#cb46-3" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (sale <span class="op">></span> <span class="dv">1000</span>) {</span>
<span id="cb46-4"><a href="#cb46-4" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> sale<span class="op">;</span> <span class="co">// Exit the function when a sale > 1000 is found</span></span>
<span id="cb46-5"><a href="#cb46-5" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb46-6"><a href="#cb46-6" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb46-7"><a href="#cb46-7" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> <span class="kw">null</span><span class="op">;</span> <span class="co">// If no sale > 1000 is found</span></span>
<span id="cb46-8"><a href="#cb46-8" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb46-9"><a href="#cb46-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb46-10"><a href="#cb46-10" aria-hidden="true" tabindex="-1"></a><span class="kw">let</span> salesAmounts <span class="op">=</span> [<span class="dv">500</span><span class="op">,</span> <span class="dv">750</span><span class="op">,</span> <span class="dv">1200</span><span class="op">,</span> <span class="dv">300</span>]<span class="op">;</span></span>
<span id="cb46-11"><a href="#cb46-11" aria-hidden="true" tabindex="-1"></a><span class="kw">let</span> largeSale <span class="op">=</span> <span class="fu">findFirstLargeSale</span>(salesAmounts)<span class="op">;</span></span>
<span id="cb46-12"><a href="#cb46-12" aria-hidden="true" tabindex="-1"></a><span class="bu">console</span><span class="op">.</span><span class="fu">log</span>(<span class="vs">`First Large Sale: </span><span class="sc">${</span>largeSale<span class="sc">}</span><span class="vs">`</span>)<span class="op">;</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</section>
<section id="output-12" class="level4">
<h4 class="anchored" data-anchor-id="output-12">Output</h4>
<pre><code>First Large Sale: 1200</code></pre>
</section>
</section>
<section id="business-examples-2" class="level3">
<h3 class="anchored" data-anchor-id="business-examples-2">Business Examples</h3>
<section id="business-example-1-validating-user-input" class="level4">
<h4 class="anchored" data-anchor-id="business-example-1-validating-user-input">Business Example 1: Validating User Input</h4>
<p>Terminate the input process if invalid data is detected.</p>
<div class="sourceCode" id="cb48"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb48-1"><a href="#cb48-1" aria-hidden="true" tabindex="-1"></a><span class="kw">function</span> <span class="fu">validateOrder</span>(order) {</span>
<span id="cb48-2"><a href="#cb48-2" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span> (<span class="kw">let</span> item <span class="kw">of</span> order<span class="op">.</span><span class="at">items</span>) {</span>
<span id="cb48-3"><a href="#cb48-3" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (item<span class="op">.</span><span class="at">quantity</span> <span class="op"><=</span> <span class="dv">0</span>) {</span>
<span id="cb48-4"><a href="#cb48-4" aria-hidden="true" tabindex="-1"></a> <span class="bu">console</span><span class="op">.</span><span class="fu">log</span>(<span class="vs">`Invalid quantity for product: </span><span class="sc">${</span>item<span class="op">.</span><span class="at">product</span><span class="sc">}</span><span class="vs">`</span>)<span class="op">;</span></span>
<span id="cb48-5"><a href="#cb48-5" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> <span class="kw">false</span><span class="op">;</span> <span class="co">// Exit the function if invalid</span></span>
<span id="cb48-6"><a href="#cb48-6" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb48-7"><a href="#cb48-7" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb48-8"><a href="#cb48-8" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> <span class="kw">true</span><span class="op">;</span> <span class="co">// All items are valid</span></span>
<span id="cb48-9"><a href="#cb48-9" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb48-10"><a href="#cb48-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb48-11"><a href="#cb48-11" aria-hidden="true" tabindex="-1"></a><span class="kw">let</span> customerOrder <span class="op">=</span> {</span>
<span id="cb48-12"><a href="#cb48-12" aria-hidden="true" tabindex="-1"></a> <span class="dt">customer</span><span class="op">:</span> <span class="st">"John Doe"</span><span class="op">,</span></span>
<span id="cb48-13"><a href="#cb48-13" aria-hidden="true" tabindex="-1"></a> <span class="dt">items</span><span class="op">:</span> [</span>
<span id="cb48-14"><a href="#cb48-14" aria-hidden="true" tabindex="-1"></a> { <span class="dt">product</span><span class="op">:</span> <span class="st">"Laptop"</span><span class="op">,</span> <span class="dt">quantity</span><span class="op">:</span> <span class="dv">2</span> }<span class="op">,</span></span>
<span id="cb48-15"><a href="#cb48-15" aria-hidden="true" tabindex="-1"></a> { <span class="dt">product</span><span class="op">:</span> <span class="st">"Mouse"</span><span class="op">,</span> <span class="dt">quantity</span><span class="op">:</span> <span class="dv">0</span> } <span class="co">// Invalid quantity</span></span>
<span id="cb48-16"><a href="#cb48-16" aria-hidden="true" tabindex="-1"></a> ]</span>
<span id="cb48-17"><a href="#cb48-17" aria-hidden="true" tabindex="-1"></a>}<span class="op">;</span></span>
<span id="cb48-18"><a href="#cb48-18" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb48-19"><a href="#cb48-19" aria-hidden="true" tabindex="-1"></a><span class="kw">let</span> isValid <span class="op">=</span> <span class="fu">validateOrder</span>(customerOrder)<span class="op">;</span></span>
<span id="cb48-20"><a href="#cb48-20" aria-hidden="true" tabindex="-1"></a><span class="bu">console</span><span class="op">.</span><span class="fu">log</span>(<span class="vs">`Order Valid: </span><span class="sc">${</span>isValid<span class="sc">}</span><span class="vs">`</span>)<span class="op">;</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<p><strong>Output:</strong></p>
<pre><code>Invalid quantity for product: Mouse
Order Valid: false</code></pre>
<p><strong>Explanation:</strong></p>
<ul>
<li>The <code>validateOrder</code> function checks each item’s quantity.</li>
<li>If an invalid quantity is found (e.g., 0 or negative), it logs an error message and exits the function using <code>return</code>.</li>
</ul>
</section>
<section id="business-example-2-skipping-incomplete-data-entries" class="level4">
<h4 class="anchored" data-anchor-id="business-example-2-skipping-incomplete-data-entries">Business Example 2: Skipping Incomplete Data Entries</h4>
<p>Use <code>continue</code> to skip processing incomplete or irrelevant data.</p>
<div class="sourceCode" id="cb50"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb50-1"><a href="#cb50-1" aria-hidden="true" tabindex="-1"></a><span class="kw">let</span> employeeRecords <span class="op">=</span> [</span>
<span id="cb50-2"><a href="#cb50-2" aria-hidden="true" tabindex="-1"></a> { <span class="dt">name</span><span class="op">:</span> <span class="st">"Alice"</span><span class="op">,</span> <span class="dt">sales</span><span class="op">:</span> <span class="dv">3000</span> }<span class="op">,</span></span>
<span id="cb50-3"><a href="#cb50-3" aria-hidden="true" tabindex="-1"></a> { <span class="dt">name</span><span class="op">:</span> <span class="st">"Bob"</span><span class="op">,</span> <span class="dt">sales</span><span class="op">:</span> <span class="kw">null</span> }<span class="op">,</span> <span class="co">// Incomplete data</span></span>
<span id="cb50-4"><a href="#cb50-4" aria-hidden="true" tabindex="-1"></a> { <span class="dt">name</span><span class="op">:</span> <span class="st">"Charlie"</span><span class="op">,</span> <span class="dt">sales</span><span class="op">:</span> <span class="dv">5000</span> }</span>
<span id="cb50-5"><a href="#cb50-5" aria-hidden="true" tabindex="-1"></a>]<span class="op">;</span></span>
<span id="cb50-6"><a href="#cb50-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb50-7"><a href="#cb50-7" aria-hidden="true" tabindex="-1"></a>employeeRecords<span class="op">.</span><span class="fu">forEach</span>((employee) <span class="kw">=></span> {</span>
<span id="cb50-8"><a href="#cb50-8" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (employee<span class="op">.</span><span class="at">sales</span> <span class="op">===</span> <span class="kw">null</span>) {</span>
<span id="cb50-9"><a href="#cb50-9" aria-hidden="true" tabindex="-1"></a> <span class="bu">console</span><span class="op">.</span><span class="fu">log</span>(<span class="vs">`Skipping </span><span class="sc">${</span>employee<span class="op">.</span><span class="at">name</span><span class="sc">}</span><span class="vs"> due to incomplete sales data.`</span>)<span class="op">;</span></span>
<span id="cb50-10"><a href="#cb50-10" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span><span class="op">;</span> <span class="co">// Skip this iteration</span></span>
<span id="cb50-11"><a href="#cb50-11" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb50-12"><a href="#cb50-12" aria-hidden="true" tabindex="-1"></a> <span class="bu">console</span><span class="op">.</span><span class="fu">log</span>(<span class="vs">`</span><span class="sc">${</span>employee<span class="op">.</span><span class="at">name</span><span class="sc">}</span><span class="vs"> has sales of $</span><span class="sc">${</span>employee<span class="op">.</span><span class="at">sales</span><span class="sc">}</span><span class="vs">`</span>)<span class="op">;</span></span>
<span id="cb50-13"><a href="#cb50-13" aria-hidden="true" tabindex="-1"></a>})<span class="op">;</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</section>
<section id="output-13" class="level4">
<h4 class="anchored" data-anchor-id="output-13">Output:</h4>
<pre><code>Alice has sales of $3000
Skipping Bob due to incomplete sales data.
Charlie has sales of $5000</code></pre>
<p><strong>Explanation:</strong></p>
<ul>
<li>The <code>forEach</code> loop processes each employee.</li>
<li>If an employee’s sales data is <code>null</code>, it logs a message and skips further processing for that employee using <code>return</code>.</li>
</ul>
<hr>
</section>
</section>
</section>
<section id="logical-operators-and-short-circuit-evaluation" class="level2">
<h2 class="anchored" data-anchor-id="logical-operators-and-short-circuit-evaluation">5. Logical Operators and Short-Circuit Evaluation</h2>
<p>Logical operators allow you to combine multiple conditions, while short-circuit evaluation optimizes the evaluation process.</p>
<section id="logical-and-and-or" class="level3">
<h3 class="anchored" data-anchor-id="logical-and-and-or">Logical AND (<code>&&</code>) and OR (<code>||</code>)</h3>
<ul>
<li><strong>Logical AND (<code>&&</code>)</strong>: Returns <code>true</code> if both operands are <code>true</code>.</li>
<li><strong>Logical OR (<code>||</code>)</strong>: Returns <code>true</code> if at least one operand is <code>true</code>.</li>
</ul>
<section id="examples" class="level4">
<h4 class="anchored" data-anchor-id="examples">Examples</h4>
<div class="sourceCode" id="cb52"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb52-1"><a href="#cb52-1" aria-hidden="true" tabindex="-1"></a><span class="kw">let</span> isMember <span class="op">=</span> <span class="kw">true</span><span class="op">;</span></span>
<span id="cb52-2"><a href="#cb52-2" aria-hidden="true" tabindex="-1"></a><span class="kw">let</span> hasCoupon <span class="op">=</span> <span class="kw">false</span><span class="op">;</span></span>
<span id="cb52-3"><a href="#cb52-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb52-4"><a href="#cb52-4" aria-hidden="true" tabindex="-1"></a><span class="co">// Logical AND</span></span>
<span id="cb52-5"><a href="#cb52-5" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> (isMember <span class="op">&&</span> hasCoupon) {</span>
<span id="cb52-6"><a href="#cb52-6" aria-hidden="true" tabindex="-1"></a> <span class="bu">console</span><span class="op">.</span><span class="fu">log</span>(<span class="st">"Apply discount."</span>)<span class="op">;</span></span>
<span id="cb52-7"><a href="#cb52-7" aria-hidden="true" tabindex="-1"></a>} <span class="cf">else</span> {</span>
<span id="cb52-8"><a href="#cb52-8" aria-hidden="true" tabindex="-1"></a> <span class="bu">console</span><span class="op">.</span><span class="fu">log</span>(<span class="st">"No discount available."</span>)<span class="op">;</span></span>
<span id="cb52-9"><a href="#cb52-9" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb52-10"><a href="#cb52-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb52-11"><a href="#cb52-11" aria-hidden="true" tabindex="-1"></a><span class="co">// Logical OR</span></span>
<span id="cb52-12"><a href="#cb52-12" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> (isMember <span class="op">||</span> hasCoupon) {</span>
<span id="cb52-13"><a href="#cb52-13" aria-hidden="true" tabindex="-1"></a> <span class="bu">console</span><span class="op">.</span><span class="fu">log</span>(<span class="st">"Eligible for discount."</span>)<span class="op">;</span></span>
<span id="cb52-14"><a href="#cb52-14" aria-hidden="true" tabindex="-1"></a>} <span class="cf">else</span> {</span>
<span id="cb52-15"><a href="#cb52-15" aria-hidden="true" tabindex="-1"></a> <span class="bu">console</span><span class="op">.</span><span class="fu">log</span>(<span class="st">"No discount available."</span>)<span class="op">;</span></span>
<span id="cb52-16"><a href="#cb52-16" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</section>
<section id="output-14" class="level4">
<h4 class="anchored" data-anchor-id="output-14">Output</h4>
<pre><code>No discount available.
Eligible for discount.</code></pre>
</section>
</section>
<section id="short-circuit-evaluation" class="level3">
<h3 class="anchored" data-anchor-id="short-circuit-evaluation">Short-Circuit Evaluation</h3>
<p>JavaScript evaluates expressions from left to right and stops as soon as the outcome is determined.</p>
<section id="example-13" class="level4">
<h4 class="anchored" data-anchor-id="example-13">Example</h4>
<div class="sourceCode" id="cb54"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb54-1"><a href="#cb54-1" aria-hidden="true" tabindex="-1"></a><span class="kw">function</span> <span class="fu">logMessage</span>(message) {</span>
<span id="cb54-2"><a href="#cb54-2" aria-hidden="true" tabindex="-1"></a> <span class="bu">console</span><span class="op">.</span><span class="fu">log</span>(message)<span class="op">;</span></span>
<span id="cb54-3"><a href="#cb54-3" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> <span class="kw">true</span><span class="op">;</span></span>
<span id="cb54-4"><a href="#cb54-4" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb54-5"><a href="#cb54-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb54-6"><a href="#cb54-6" aria-hidden="true" tabindex="-1"></a><span class="kw">let</span> condition <span class="op">=</span> <span class="kw">false</span><span class="op">;</span></span>
<span id="cb54-7"><a href="#cb54-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb54-8"><a href="#cb54-8" aria-hidden="true" tabindex="-1"></a><span class="co">// Short-circuit with AND</span></span>
<span id="cb54-9"><a href="#cb54-9" aria-hidden="true" tabindex="-1"></a>condition <span class="op">&&</span> <span class="fu">logMessage</span>(<span class="st">"This will not be logged."</span>)<span class="op">;</span></span>
<span id="cb54-10"><a href="#cb54-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb54-11"><a href="#cb54-11" aria-hidden="true" tabindex="-1"></a><span class="co">// Short-circuit with OR</span></span>
<span id="cb54-12"><a href="#cb54-12" aria-hidden="true" tabindex="-1"></a>condition <span class="op">||</span> <span class="fu">logMessage</span>(<span class="st">"This will be logged."</span>)<span class="op">;</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</section>
<section id="output-15" class="level4">
<h4 class="anchored" data-anchor-id="output-15">Output</h4>
<pre><code>This will be logged.</code></pre>
<p><strong>Explanation:</strong></p>
<ul>
<li>The <code>&&</code> operator stops evaluating when the first operand (<code>condition</code>) is <code>false</code>, so <code>logMessage</code> is not called.</li>
<li>The <code>||</code> operator calls <code>logMessage</code> because the first operand (<code>condition</code>) is <code>false</code>.</li>
</ul>
</section>
</section>
<section id="business-examples-3" class="level3">
<h3 class="anchored" data-anchor-id="business-examples-3">Business Examples</h3>
<section id="business-example-1-conditional-feature-activation" class="level4">
<h4 class="anchored" data-anchor-id="business-example-1-conditional-feature-activation">Business Example 1: Conditional Feature Activation</h4>
<p>Activate features based on user role and subscription status.</p>
<div class="sourceCode" id="cb56"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb56-1"><a href="#cb56-1" aria-hidden="true" tabindex="-1"></a><span class="kw">let</span> userRole <span class="op">=</span> <span class="st">"admin"</span><span class="op">;</span></span>
<span id="cb56-2"><a href="#cb56-2" aria-hidden="true" tabindex="-1"></a><span class="kw">let</span> isSubscribed <span class="op">=</span> <span class="kw">true</span><span class="op">;</span></span>
<span id="cb56-3"><a href="#cb56-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb56-4"><a href="#cb56-4" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> (userRole <span class="op">===</span> <span class="st">"admin"</span> <span class="op">&&</span> isSubscribed) {</span>
<span id="cb56-5"><a href="#cb56-5" aria-hidden="true" tabindex="-1"></a> <span class="bu">console</span><span class="op">.</span><span class="fu">log</span>(<span class="st">"Access granted to advanced analytics dashboard."</span>)<span class="op">;</span></span>
<span id="cb56-6"><a href="#cb56-6" aria-hidden="true" tabindex="-1"></a>} <span class="cf">else</span> {</span>
<span id="cb56-7"><a href="#cb56-7" aria-hidden="true" tabindex="-1"></a> <span class="bu">console</span><span class="op">.</span><span class="fu">log</span>(<span class="st">"Access denied."</span>)<span class="op">;</span></span>
<span id="cb56-8"><a href="#cb56-8" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</section>
<section id="output-16" class="level4">
<h4 class="anchored" data-anchor-id="output-16">Output:</h4>
<pre><code>Access granted to advanced analytics dashboard.</code></pre>
<p><strong>Explanation:</strong></p>
<ul>
<li>Both conditions (<code>userRole === "admin"</code> and <code>isSubscribed</code>) must be <code>true</code> to grant access.</li>
</ul>
</section>
<section id="business-example-2-efficient-data-validation" class="level4">
<h4 class="anchored" data-anchor-id="business-example-2-efficient-data-validation">Business Example 2: Efficient Data Validation</h4>
<p>Use short-circuit evaluation to validate data before processing.</p>
<div class="sourceCode" id="cb58"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb58-1"><a href="#cb58-1" aria-hidden="true" tabindex="-1"></a><span class="kw">function</span> <span class="fu">processOrder</span>(order) {</span>
<span id="cb58-2"><a href="#cb58-2" aria-hidden="true" tabindex="-1"></a> <span class="co">// Check if order and customer information exist</span></span>
<span id="cb58-3"><a href="#cb58-3" aria-hidden="true" tabindex="-1"></a> order <span class="op">&&</span> order<span class="op">.</span><span class="at">customer</span> <span class="op">&&</span> order<span class="op">.</span><span class="at">items</span> <span class="op">&&</span> <span class="bu">process</span>(order)<span class="op">;</span></span>
<span id="cb58-4"><a href="#cb58-4" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb58-5"><a href="#cb58-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb58-6"><a href="#cb58-6" aria-hidden="true" tabindex="-1"></a><span class="kw">function</span> <span class="fu">process</span>(order) {</span>
<span id="cb58-7"><a href="#cb58-7" aria-hidden="true" tabindex="-1"></a> <span class="bu">console</span><span class="op">.</span><span class="fu">log</span>(<span class="vs">`Processing order for </span><span class="sc">${</span>order<span class="op">.</span><span class="at">customer</span><span class="op">.</span><span class="at">name</span><span class="sc">}</span><span class="vs">`</span>)<span class="op">;</span></span>
<span id="cb58-8"><a href="#cb58-8" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb58-9"><a href="#cb58-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb58-10"><a href="#cb58-10" aria-hidden="true" tabindex="-1"></a><span class="kw">let</span> validOrder <span class="op">=</span> {</span>
<span id="cb58-11"><a href="#cb58-11" aria-hidden="true" tabindex="-1"></a> <span class="dt">customer</span><span class="op">:</span> { <span class="dt">name</span><span class="op">:</span> <span class="st">"Alice"</span> }<span class="op">,</span></span>
<span id="cb58-12"><a href="#cb58-12" aria-hidden="true" tabindex="-1"></a> <span class="dt">items</span><span class="op">:</span> [<span class="st">"Laptop"</span><span class="op">,</span> <span class="st">"Mouse"</span>]</span>
<span id="cb58-13"><a href="#cb58-13" aria-hidden="true" tabindex="-1"></a>}<span class="op">;</span></span>
<span id="cb58-14"><a href="#cb58-14" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb58-15"><a href="#cb58-15" aria-hidden="true" tabindex="-1"></a><span class="kw">let</span> invalidOrder <span class="op">=</span> <span class="kw">null</span><span class="op">;</span></span>
<span id="cb58-16"><a href="#cb58-16" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb58-17"><a href="#cb58-17" aria-hidden="true" tabindex="-1"></a><span class="fu">processOrder</span>(validOrder)<span class="op">;</span> <span class="co">// Outputs: Processing order for Alice</span></span>
<span id="cb58-18"><a href="#cb58-18" aria-hidden="true" tabindex="-1"></a><span class="fu">processOrder</span>(invalidOrder)<span class="op">;</span> <span class="co">// No output, as the condition fails</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<p><strong>Explanation:</strong></p>
<ul>
<li>The <code>processOrder</code> function only calls <code>process(order)</code> if all conditions are <code>true</code>.</li>
<li>If <code>order</code> is <code>null</code>, the function short-circuits and does not attempt to process the order, preventing errors.</li>
</ul>
<hr>
</section>
</section>
</section>
<section id="nested-control-structures" class="level2">
<h2 class="anchored" data-anchor-id="nested-control-structures">6. Nested Control Structures</h2>
<p>Nesting control structures involves placing one control structure inside another, allowing for more complex decision-making and iteration.</p>
<section id="concept-of-nesting" class="level3">
<h3 class="anchored" data-anchor-id="concept-of-nesting">Concept of Nesting</h3>
<p>Nesting can occur with any combination of control structures, such as an <code>if</code> statement inside a <code>for</code> loop or a <code>for</code> loop inside an <code>if</code> statement.</p>
</section>
<section id="best-practices-for-nested-structures" class="level3">
<h3 class="anchored" data-anchor-id="best-practices-for-nested-structures">Best Practices for Nested Structures</h3>
<ul>
<li><strong>Limit Depth</strong>: Avoid excessive nesting to maintain code readability and maintainability.</li>
<li><strong>Use Functions</strong>: Break down complex nested structures into smaller functions.</li>
<li><strong>Consistent Indentation</strong>: Properly indent nested code blocks for clarity.</li>
</ul>
</section>
<section id="business-examples-4" class="level3">
<h3 class="anchored" data-anchor-id="business-examples-4">Business Examples</h3>
<section id="business-example-1-multi-level-discount-calculations" class="level4">
<h4 class="anchored" data-anchor-id="business-example-1-multi-level-discount-calculations">Business Example 1: Multi-Level Discount Calculations</h4>
<p>Apply different discount tiers based on customer type and total purchase amount.</p>
<div class="sourceCode" id="cb59"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb59-1"><a href="#cb59-1" aria-hidden="true" tabindex="-1"></a><span class="kw">function</span> <span class="fu">calculateDiscount</span>(customerType<span class="op">,</span> totalAmount) {</span>
<span id="cb59-2"><a href="#cb59-2" aria-hidden="true" tabindex="-1"></a> <span class="kw">let</span> discount <span class="op">=</span> <span class="dv">0</span><span class="op">;</span></span>
<span id="cb59-3"><a href="#cb59-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb59-4"><a href="#cb59-4" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (customerType <span class="op">===</span> <span class="st">"VIP"</span>) {</span>
<span id="cb59-5"><a href="#cb59-5" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (totalAmount <span class="op">></span> <span class="dv">1000</span>) {</span>
<span id="cb59-6"><a href="#cb59-6" aria-hidden="true" tabindex="-1"></a> discount <span class="op">=</span> <span class="dv">20</span><span class="op">;</span> <span class="co">// 20% discount for VIPs over $1000</span></span>
<span id="cb59-7"><a href="#cb59-7" aria-hidden="true" tabindex="-1"></a> } <span class="cf">else</span> {</span>
<span id="cb59-8"><a href="#cb59-8" aria-hidden="true" tabindex="-1"></a> discount <span class="op">=</span> <span class="dv">15</span><span class="op">;</span> <span class="co">// 15% discount for VIPs up to $1000</span></span>
<span id="cb59-9"><a href="#cb59-9" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb59-10"><a href="#cb59-10" aria-hidden="true" tabindex="-1"></a> } <span class="cf">else</span> <span class="cf">if</span> (customerType <span class="op">===</span> <span class="st">"Regular"</span>) {</span>
<span id="cb59-11"><a href="#cb59-11" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (totalAmount <span class="op">></span> <span class="dv">1000</span>) {</span>
<span id="cb59-12"><a href="#cb59-12" aria-hidden="true" tabindex="-1"></a> discount <span class="op">=</span> <span class="dv">10</span><span class="op">;</span> <span class="co">// 10% discount for Regular customers over $1000</span></span>
<span id="cb59-13"><a href="#cb59-13" aria-hidden="true" tabindex="-1"></a> } <span class="cf">else</span> {</span>
<span id="cb59-14"><a href="#cb59-14" aria-hidden="true" tabindex="-1"></a> discount <span class="op">=</span> <span class="dv">5</span><span class="op">;</span> <span class="co">// 5% discount for Regular customers up to $1000</span></span>
<span id="cb59-15"><a href="#cb59-15" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb59-16"><a href="#cb59-16" aria-hidden="true" tabindex="-1"></a> } <span class="cf">else</span> {</span>
<span id="cb59-17"><a href="#cb59-17" aria-hidden="true" tabindex="-1"></a> discount <span class="op">=</span> <span class="dv">0</span><span class="op">;</span> <span class="co">// No discount for others</span></span>
<span id="cb59-18"><a href="#cb59-18" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb59-19"><a href="#cb59-19" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb59-20"><a href="#cb59-20" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> discount<span class="op">;</span></span>
<span id="cb59-21"><a href="#cb59-21" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb59-22"><a href="#cb59-22" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb59-23"><a href="#cb59-23" aria-hidden="true" tabindex="-1"></a><span class="kw">let</span> discount1 <span class="op">=</span> <span class="fu">calculateDiscount</span>(<span class="st">"VIP"</span><span class="op">,</span> <span class="dv">1500</span>)<span class="op">;</span></span>
<span id="cb59-24"><a href="#cb59-24" aria-hidden="true" tabindex="-1"></a><span class="kw">let</span> discount2 <span class="op">=</span> <span class="fu">calculateDiscount</span>(<span class="st">"Regular"</span><span class="op">,</span> <span class="dv">800</span>)<span class="op">;</span></span>
<span id="cb59-25"><a href="#cb59-25" aria-hidden="true" tabindex="-1"></a><span class="kw">let</span> discount3 <span class="op">=</span> <span class="fu">calculateDiscount</span>(<span class="st">"Guest"</span><span class="op">,</span> <span class="dv">500</span>)<span class="op">;</span></span>
<span id="cb59-26"><a href="#cb59-26" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb59-27"><a href="#cb59-27" aria-hidden="true" tabindex="-1"></a><span class="bu">console</span><span class="op">.</span><span class="fu">log</span>(<span class="vs">`VIP Discount: </span><span class="sc">${</span>discount1<span class="sc">}</span><span class="vs">%`</span>)<span class="op">;</span> <span class="co">// Output: VIP Discount: 20%</span></span>
<span id="cb59-28"><a href="#cb59-28" aria-hidden="true" tabindex="-1"></a><span class="bu">console</span><span class="op">.</span><span class="fu">log</span>(<span class="vs">`Regular Discount: </span><span class="sc">${</span>discount2<span class="sc">}</span><span class="vs">%`</span>)<span class="op">;</span> <span class="co">// Output: Regular Discount: 5%</span></span>
<span id="cb59-29"><a href="#cb59-29" aria-hidden="true" tabindex="-1"></a><span class="bu">console</span><span class="op">.</span><span class="fu">log</span>(<span class="vs">`Guest Discount: </span><span class="sc">${</span>discount3<span class="sc">}</span><span class="vs">%`</span>)<span class="op">;</span> <span class="co">// Output: Guest Discount: 0%</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<p><strong>Explanation:</strong></p>
<ul>
<li>The function <code>calculateDiscount</code> uses nested <code>if</code> statements to determine the appropriate discount based on customer type and purchase amount.</li>
</ul>
</section>
<section id="business-example-2-complex-inventory-management" class="level4">
<h4 class="anchored" data-anchor-id="business-example-2-complex-inventory-management">Business Example 2: Complex Inventory Management</h4>
<p>Check inventory levels and reorder products if stock is below a threshold, considering supplier reliability.</p>
<div class="sourceCode" id="cb60"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb60-1"><a href="#cb60-1" aria-hidden="true" tabindex="-1"></a><span class="kw">function</span> <span class="fu">manageInventory</span>(product) {</span>
<span id="cb60-2"><a href="#cb60-2" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (product<span class="op">.</span><span class="at">stock</span> <span class="op"><</span> product<span class="op">.</span><span class="at">minStock</span>) {</span>
<span id="cb60-3"><a href="#cb60-3" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (product<span class="op">.</span><span class="at">supplierReliability</span> <span class="op">===</span> <span class="st">"high"</span>) {</span>
<span id="cb60-4"><a href="#cb60-4" aria-hidden="true" tabindex="-1"></a> <span class="bu">console</span><span class="op">.</span><span class="fu">log</span>(<span class="vs">`Reordering </span><span class="sc">${</span>product<span class="op">.</span><span class="at">name</span><span class="sc">}</span><span class="vs"> from reliable supplier.`</span>)<span class="op">;</span></span>
<span id="cb60-5"><a href="#cb60-5" aria-hidden="true" tabindex="-1"></a> } <span class="cf">else</span> {</span>
<span id="cb60-6"><a href="#cb60-6" aria-hidden="true" tabindex="-1"></a> <span class="bu">console</span><span class="op">.</span><span class="fu">log</span>(<span class="vs">`Reordering </span><span class="sc">${</span>product<span class="op">.</span><span class="at">name</span><span class="sc">}</span><span class="vs"> from alternate supplier.`</span>)<span class="op">;</span></span>
<span id="cb60-7"><a href="#cb60-7" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb60-8"><a href="#cb60-8" aria-hidden="true" tabindex="-1"></a> } <span class="cf">else</span> {</span>
<span id="cb60-9"><a href="#cb60-9" aria-hidden="true" tabindex="-1"></a> <span class="bu">console</span><span class="op">.</span><span class="fu">log</span>(<span class="vs">`</span><span class="sc">${</span>product<span class="op">.</span><span class="at">name</span><span class="sc">}</span><span class="vs"> stock is sufficient.`</span>)<span class="op">;</span></span>
<span id="cb60-10"><a href="#cb60-10" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb60-11"><a href="#cb60-11" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb60-12"><a href="#cb60-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb60-13"><a href="#cb60-13" aria-hidden="true" tabindex="-1"></a><span class="kw">let</span> products <span class="op">=</span> [</span>
<span id="cb60-14"><a href="#cb60-14" aria-hidden="true" tabindex="-1"></a> { <span class="dt">name</span><span class="op">:</span> <span class="st">"Laptop"</span><span class="op">,</span> <span class="dt">stock</span><span class="op">:</span> <span class="dv">5</span><span class="op">,</span> <span class="dt">minStock</span><span class="op">:</span> <span class="dv">10</span><span class="op">,</span> <span class="dt">supplierReliability</span><span class="op">:</span> <span class="st">"high"</span> }<span class="op">,</span></span>
<span id="cb60-15"><a href="#cb60-15" aria-hidden="true" tabindex="-1"></a> { <span class="dt">name</span><span class="op">:</span> <span class="st">"Mouse"</span><span class="op">,</span> <span class="dt">stock</span><span class="op">:</span> <span class="dv">15</span><span class="op">,</span> <span class="dt">minStock</span><span class="op">:</span> <span class="dv">10</span><span class="op">,</span> <span class="dt">supplierReliability</span><span class="op">:</span> <span class="st">"low"</span> }<span class="op">,</span></span>
<span id="cb60-16"><a href="#cb60-16" aria-hidden="true" tabindex="-1"></a> { <span class="dt">name</span><span class="op">:</span> <span class="st">"Keyboard"</span><span class="op">,</span> <span class="dt">stock</span><span class="op">:</span> <span class="dv">8</span><span class="op">,</span> <span class="dt">minStock</span><span class="op">:</span> <span class="dv">10</span><span class="op">,</span> <span class="dt">supplierReliability</span><span class="op">:</span> <span class="st">"medium"</span> }</span>
<span id="cb60-17"><a href="#cb60-17" aria-hidden="true" tabindex="-1"></a>]<span class="op">;</span></span>
<span id="cb60-18"><a href="#cb60-18" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb60-19"><a href="#cb60-19" aria-hidden="true" tabindex="-1"></a>products<span class="op">.</span><span class="fu">forEach</span>(product <span class="kw">=></span> <span class="fu">manageInventory</span>(product))<span class="op">;</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</section>
<section id="output-17" class="level4">
<h4 class="anchored" data-anchor-id="output-17">Output:</h4>
<pre><code>Reordering Laptop from reliable supplier.
Mouse stock is sufficient.
Keyboard stock is sufficient.</code></pre>
<p><strong>Explanation:</strong></p>
<ul>
<li>The <code>manageInventory</code> function uses nested <code>if</code> statements to decide whether to reorder products based on stock levels and supplier reliability.</li>
<li>In this example, “Mouse” has sufficient stock, while “Laptop” is reordered from the reliable supplier due to low stock.</li>
</ul>
<hr>
</section>
</section>
</section>
<section id="conclusion" class="level2">
<h2 class="anchored" data-anchor-id="conclusion">7. Conclusion</h2>
<p>Control structures are the backbone of any JavaScript application, enabling developers to create dynamic, efficient, and responsive business solutions. By mastering conditional statements, loops, control flow interruptions, logical operators, and nested structures, you can build robust applications that handle complex business logic with ease.</p>
<section id="key-takeaways" class="level3">
<h3 class="anchored" data-anchor-id="key-takeaways">Key Takeaways:</h3>
<ul>
<li><strong>Conditional Statements</strong>: Use <code>if</code>, <code>else if</code>, <code>else</code>, <code>switch</code>, and the ternary operator to make decisions based on varying conditions.</li>
<li><strong>Loops</strong>: Utilize <code>for</code>, <code>while</code>, <code>do...while</code>, <code>for...in</code>, <code>for...of</code>, and <code>forEach</code> to iterate over data structures and perform repetitive tasks efficiently.</li>
<li><strong>Control Flow Interruptions</strong>: Employ <code>break</code>, <code>continue</code>, and <code>return</code> to manage the flow within loops and functions, allowing for early exits or skipped iterations.</li>
<li><strong>Logical Operators and Short-Circuit Evaluation</strong>: Combine multiple conditions using <code>&&</code> and <code>||</code>, and leverage short-circuit evaluation for efficient condition checking.</li>
<li><strong>Nested Control Structures</strong>: Combine multiple control structures to handle complex business scenarios, while adhering to best practices to maintain code readability.</li>
</ul>
</section>
<section id="best-practices-in-using-control-structures" class="level3">
<h3 class="anchored" data-anchor-id="best-practices-in-using-control-structures">Best Practices in Using Control Structures:</h3>
<ul>
<li><strong>Maintain Readability</strong>: Write clear and concise code by avoiding overly nested structures and using proper indentation.</li>
<li><strong>Modularize Code</strong>: Break down complex logic into smaller, reusable functions to enhance maintainability and scalability.</li>
<li><strong>Optimize Performance</strong>: Use appropriate loops and control structures to minimize unnecessary computations and enhance application performance.</li>
</ul>
<p>Understanding and effectively utilizing control structures in JavaScript empowers you to build sophisticated business applications capable of handling diverse and intricate operational requirements.</p>
<p><strong>Happy coding!</strong></p>
</section>
</section>
</section>
</main>
<!-- /main column -->
<script id="quarto-html-after-body" type="application/javascript">
window.document.addEventListener("DOMContentLoaded", function (event) {
const toggleBodyColorMode = (bsSheetEl) => {
const mode = bsSheetEl.getAttribute("data-mode");
const bodyEl = window.document.querySelector("body");
if (mode === "dark") {
bodyEl.classList.add("quarto-dark");
bodyEl.classList.remove("quarto-light");
} else {
bodyEl.classList.add("quarto-light");
bodyEl.classList.remove("quarto-dark");
}
}
const toggleBodyColorPrimary = () => {
const bsSheetEl = window.document.querySelector("link#quarto-bootstrap");
if (bsSheetEl) {
toggleBodyColorMode(bsSheetEl);
}
}
toggleBodyColorPrimary();
const icon = "";
const anchorJS = new window.AnchorJS();
anchorJS.options = {
placement: 'right',
icon: icon
};
anchorJS.add('.anchored');
const isCodeAnnotation = (el) => {
for (const clz of el.classList) {
if (clz.startsWith('code-annotation-')) {
return true;
}
}
return false;
}
const onCopySuccess = function(e) {
// button target
const button = e.trigger;
// don't keep focus
button.blur();
// flash "checked"
button.classList.add('code-copy-button-checked');
var currentTitle = button.getAttribute("title");
button.setAttribute("title", "Copied!");
let tooltip;
if (window.bootstrap) {
button.setAttribute("data-bs-toggle", "tooltip");
button.setAttribute("data-bs-placement", "left");
button.setAttribute("data-bs-title", "Copied!");
tooltip = new bootstrap.Tooltip(button,
{ trigger: "manual",
customClass: "code-copy-button-tooltip",
offset: [0, -8]});
tooltip.show();
}
setTimeout(function() {
if (tooltip) {
tooltip.hide();
button.removeAttribute("data-bs-title");
button.removeAttribute("data-bs-toggle");
button.removeAttribute("data-bs-placement");
}
button.setAttribute("title", currentTitle);
button.classList.remove('code-copy-button-checked');
}, 1000);
// clear code selection
e.clearSelection();
}
const getTextToCopy = function(trigger) {