-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecap.js
1329 lines (1189 loc) · 39.6 KB
/
recap.js
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
// ==========Hello World Program==========
{
console.log("Hello World");
document.writeln("Hello World");
}
// ==========Comment==========
/*
- //
*/
// ========== Tipe Data Number ==========
{
document.writeln("Bulat " + 100 * 100);
document.writeln("<br>");
document.writeln("Pecahan " + 100.5 * 100);
document.writeln("<br>");
document.writeln("Binary " + 0b110011);
}
// ========== Tipe Data Boolean ==========
{
document.writeln("<br>" + "Boolean : " + true);
document.writeln("<br>" + "Boolean : " + false);
}
// ========== Tipe Data String ==========
{
document.writeln("<br>" + "Fauzan Ahmad");
document.writeln("<br>" + "Programmer Kelaperan" + "<br>");
document.write("<textarea cols='100' rows='10'>");
document.write("Programmer Kelaperan\n");
document.write("\tAda video tiap minggu katanya\n");
document.write("\\Dia juga bakalan\\bikin tutorial design\n");
document.write('"Masak-masak dan seputar tips lainnya"\n');
document.write("</textarea>");
}
// ========== Variable ==========
{
let fullName = "Fauzan Ahmad",
nickName = "Ujang",
passion;
for (let i = 1; i <= 5; i++) {
passion = "Design";
document.writeln(
i + ". " + fullName + " " + nickName + " " + passion + "<br>"
);
}
document.writeln(fullName + "<br>");
document.writeln(fullName + "<br>");
document.writeln(fullName + "<br>");
fullName = "hartono"; // ubah value
document.writeln(fullName + "<br>");
document.writeln(fullName + "<br>");
document.writeln(fullName + "<br>");
document.writeln(fullName + "<br>");
document.writeln(fullName + "<br>");
document.writeln(fullName + "<br>");
const app = "Belajar yang rajin";
// app = "males mom"; tidak boleh diubah
document.writeln(app + "<br>");
}
// ========== Operator Matematika ==========
{
document.writeln(
"<p>======================Operator Matematika=====================</p>"
);
let result = 1 + 2;
document.writeln("<p>1 + 2 = " + result + "</p>");
let originalResult = result;
result = result - 1;
document.writeln("<p>" + originalResult + " - 1 = " + result + "</p>");
originalResult = result;
result = result * 2;
document.writeln("<p>" + originalResult + " * 2 = " + result + "</p>");
originalResult = result;
result = result ** 2;
document.writeln("<p>" + originalResult + " ^ 2 = " + result + "</p>");
originalResult = result;
result = result / 3;
document.writeln("<p>" + originalResult + " / 3 = " + result + "</p>");
originalResult = result;
document.writeln(
"<p>====================Operator Augmented Assignment=======================</p>"
);
result = 1 + 2;
document.writeln("<p>1 + 2 = " + result + "</p>");
originalResult = result;
result -= 1;
document.writeln("<p>" + originalResult + " - 1 = " + result + "</p>");
originalResult = result;
result *= 2;
document.writeln("<p>" + originalResult + " * 2 = " + result + "</p>");
originalResult = result;
result **= 2;
document.writeln("<p>" + originalResult + " ^ 2 = " + result + "</p>");
originalResult = result;
result %= 3;
document.writeln("<p>" + originalResult + " ^ 2 = " + result + "</p>");
originalResult = result;
result /= 3;
document.writeln("<p>" + originalResult + " / 3 = " + result + "</p>");
originalResult = result;
document.writeln("<p>===========================================</p>");
}
// ========== Operator Perbandingan ==========
{
document.writeln("<p> =========== && / Dan ===========</p>");
let result = true && true;
document.writeln("<p> true && true = " + result + "</p>");
result = true && false;
document.writeln("<p> true && false = " + result + "</p>");
result = false && true;
document.writeln("<p> false && true = " + result + "</p>");
result = false && false;
document.writeln("<p> false && false = " + result + "</p>");
document.writeln("<p> =========== || / Atau ===========</p>");
result = true || true;
document.writeln("<p> true || true = " + result + "</p>");
result = true || false;
document.writeln("<p> true || false = " + result + "</p>");
result = false || true;
document.writeln("<p> false || true = " + result + "</p>");
result = false || false;
document.writeln("<p> false || false = " + result + "</p>");
document.writeln(
"<p> =========== ! (operator unary) / Kebalikan ===========</p>"
);
result = !true;
document.writeln("<p> !true = " + result + "</p>");
result = !false;
document.writeln("<p> !false = " + result + "</p>");
document.writeln("<p> =========== contoh logika ===========</p>");
const nilaiUjian = 76,
nilaiAbsensi = 76,
lulusUjian = nilaiUjian > 75,
lulusAbsensi = nilaiAbsensi > 75;
document.writeln("<p> nilai ujian = " + nilaiUjian + " </p>");
document.writeln("<p> nilai absensi = " + nilaiAbsensi + " </p>");
document.writeln("<p> lulus ujian = " + lulusUjian + " </p>");
document.writeln("<p> lulus absensi = " + lulusAbsensi + " </p>");
const lulus = lulusUjian && lulusAbsensi;
document.writeln(
"<p> Saya ... (true=lulus, false=tidak lulus) = <strong>" +
lulus +
"</strong></p>"
);
}
// ========== Operator Logika ==========
{
document.writeln("<p> =========== && / Dan ===========</p>");
let result = true && true;
document.writeln("<p> true && true = " + result + "</p>");
result = true && false;
document.writeln("<p> true && false = " + result + "</p>");
result = false && true;
document.writeln("<p> false && true = " + result + "</p>");
result = false && false;
document.writeln("<p> false && false = " + result + "</p>");
document.writeln("<p> =========== || / Atau ===========</p>");
result = true || true;
document.writeln("<p> true || true = " + result + "</p>");
result = true || false;
document.writeln("<p> true || false = " + result + "</p>");
result = false || true;
document.writeln("<p> false || true = " + result + "</p>");
result = false || false;
document.writeln("<p> false || false = " + result + "</p>");
document.writeln(
"<p> =========== ! (operator unary) / Kebalikan ===========</p>"
);
result = !true;
document.writeln("<p> !true = " + result + "</p>");
result = !false;
document.writeln("<p> !false = " + result + "</p>");
document.writeln("<p> =========== contoh logika ===========</p>");
const nilaiUjian = 76,
nilaiAbsensi = 76,
lulusUjian = nilaiUjian > 75,
lulusAbsensi = nilaiAbsensi > 75;
document.writeln("<p> nilai ujian = " + nilaiUjian + " </p>");
document.writeln("<p> nilai absensi = " + nilaiAbsensi + " </p>");
document.writeln("<p> lulus ujian = " + lulusUjian + " </p>");
document.writeln("<p> lulus absensi = " + lulusAbsensi + " </p>");
const lulus = lulusUjian && lulusAbsensi;
document.writeln(
"<p> Saya ... (true=lulus, false=tidak lulus) = <strong>" +
lulus +
"</strong></p>"
);
}
// ========== Console ==========
{
document.writeln("<p><strong>Press F12 atau inspect/console</strong></p>");
console.debug("Hello World,Debug");
console.info("Hello World");
console.warn("Hello World, Ini peringatan Loh !");
console.error("Hello World, Ini error loh !");
}
// ========== String Template ==========
{
const name = "Fauzan Ahmad";
const hobby = "makan nasi uduk";
const template = `Name : ${name}, Hobby : ${hobby}`;
document.writeln("<p><strong>Press F12 atau inspect/console</strong></p>");
console.info(template);
// With expression ">"
const nilai = 81;
const templateTwo = `Name : ${name}, Lulus : ${nilai > 80}`;
console.info(templateTwo);
}
// ========== Konversi String dan Number ==========
{
document.writeln("<p><strong>Press F12 or inspect/console</strong></p>");
document.writeln(
"<p><strong>==============Konversi String dan Number==============</strong></p>"
);
const val1 = parseInt("2");
const val2 = 1;
const sum = val1 + val2;
document.writeln(`<p>${sum}</p>`);
document.writeln(`<p>${parseInt("1.3")}</p>`);
document.writeln(`<p>${parseFloat("1.3")}</p>`);
document.writeln(`<p>${Number("1.3")}</p>`);
let a = 1;
let b = 2;
let total = a.toString() + b;
document.writeln(`total ${total}`);
document.writeln(
"<p><strong>==============NaN (Not a Number)==============</strong></p>"
);
document.writeln(`<p>${parseInt("salah")}</p>`);
document.writeln(`<p>${parseFloat("1.3Text Loh")}</p>`);
// Number(string) tidak mentolerir kesalahan pada data, berbeda dengan parseInt atau parseFloat yang masih mentolerir kesalahan data
document.writeln(`<p>${Number("1.3Text Loh")}</p>`);
document.writeln(
"<p><strong>==============Operasi pada NaN (Not a Number)==============</strong></p>"
);
a = Number("salah");
total = a + 2;
document.writeln(
`<p> total dari Number("salah") + 2 = <strong> ${total}</strong></p>`
);
document.writeln(
"<p><strong>==============isNaN Function==============</strong></p>"
);
a = Number("salah");
total = a + 2;
document.writeln(
`<p> isNaN("salah") = <strong> ${isNaN("salah")}</strong></p>`
);
document.writeln(`<p> isNaN(${b}) = <strong> ${isNaN(b)}</strong></p>`);
document.writeln(`<p> isNaN(NaN) = <strong> ${isNaN(NaN)}</strong></p>`);
}
// ========== Tipe Data Array ==========
// ========== Tipe Data Object ==========
// ========== If Expression ==========
{
const examValue = 60;
if (examValue > 80) {
document.writeln(`<p>Good Job</p>`);
} else if (examValue > 60) {
document.writeln(`<p>Not Bad</p>`);
} else {
document.writeln(`<p>Try Again Mas !</p>`);
alert(
`Bener2 deh anda ${prompt("Belajar lagi besok, masukan NIM mu mas !!!")}`
);
}
}
// ========== Popup ==========
{
alert("Halo dunia !!");
const nama = prompt("Whats your name sir ?");
alert(`My name is ${nama}`);
const masuk = confirm("Anda yakin masuk ? Didalam gelap loh!!");
if (masuk) {
const nama = prompt("Siapa lagi namamu ?");
alert(`Namas saya ${nama}`);
} else {
alert("Bye bye !");
}
}
// ========== Undefined ==========
{
document.writeln("<p><strong>Press F12 to open the console</strong></p>");
let name;
if (name === undefined) {
console.info(`name undefined`);
} else {
console.info(`name defined is ${name}`);
}
console.info("======Array Undefined Down Bellow======");
const nameArray = ["Fauzan", "Ahmad", "Gokil"];
if (nameArray[2] === undefined) {
console.info(`name undefined`);
} else {
console.info(`name defined is ${nameArray[2]}`);
}
console.info("======Object Undefined Down Bellow======");
let nameObject = {
name: "fauzan ahmad",
};
if (nameObject.name === undefined) {
console.info(`name undefined`);
} else {
console.info(`name defined is ${nameObject.name}`);
}
}
// ========== Null ==========
{
let name = null;
if (name === undefined) {
alert("Hello Undefined");
} else if (name === null) {
alert("Hello Null");
} else {
alert(`Hello ${name}`);
}
}
// ========== Switch Expression ==========
{
let nilai = prompt(`Selamat Kamu Telah Ujian. Masukan Nilai :`);
// alert(nilai);
switch (nilai) {
case "A":
alert("Wow anda lulus dengan baik !");
break;
case "B":
case "C":
alert("Anda lulus tuh");
break;
case "D":
alert("Anda tidak lulus, silahkan ikut semester pendek, hahaha");
break;
default:
alert("Mungkin anda salah jurusan !");
}
}
// ========== Operator typeof ==========
{
let data = null;
switch (typeof data) {
case "number":
document.write(`Tipe data yang kamu uji adalah number = ${typeof data}`);
break;
case "string":
document.write(`Tipe data yang kamu uji adalah string = ${typeof data}`);
break;
case "boolean":
document.write(`Tipe data yang kamu uji adalah boolean = ${typeof data}`);
break;
case "null":
document.write(`Tipe data yang kamu uji adalah null = ${typeof data}`);
break;
case "undefined":
document.write(`Tipe data yang kamu uji adalah undefined = ${typeof data}`);
break;
case "symbol":
document.write(`Tipe data yang kamu uji adalah symbol = ${typeof data}`);
break;
case "function":
document.write(`Tipe data yang kamu uji adalah function = ${typeof data}`);
break;
case "bigint":
document.write(`Tipe data yang kamu uji adalah bigint = ${typeof data}`);
break;
default:
document.write(`Tipe data yang kamu uji adalah object = ${typeof data}`);
}
}
// ========== Operator in ==========
{
let person = {
firstName: "Fauzan",
lastName: "Ahmad",
};
if ("firstName" in person) {
alert(`Hello ${person.firstName}`);
} else {
alert(`Hello `);
}
let result = "firstName" in person;
document.writeln(`<p>${result}</p>`);
document.writeln(`<p>===========================</p>`);
person = {
lastName: undefined,
};
result = "lastName" in person;
document.writeln(`<p>${result}</p>`);
document.writeln(`<p>===========================</p>`);
person = [null, "Fauzan", null];
result = 0 in person;
document.writeln(`<p>${result}</p>`);
}
// ========== Ternary Operator ==========
{
const nilai = 75;
let ucapan;
document.writeln(`<p>=====Tanpa Menggunakan Ternary Operator=====</p>`);
if (nilai >= 75) {
ucapan = "Selamat Anda Lulus";
} else {
ucapan = "Silahkan coba lagi tahun depan di Next Master Chef !";
}
document.writeln(`<p>${ucapan}</p>`);
document.writeln(`<p>=====Dengan Menggunakan Ternary Operator=====</p>`);
ucapan =
nilai >= 75 ?
"Selamat Anda Lulus" :
"Silahkan coba lagi tahun depan di Next Master Chef !";
document.writeln(`<p>${ucapan}</p>`);
}
// ========== Nullish Coalescing Operator ==========
{
let parameter;
let data = parameter;
document.writeln(`<p>=====Tanpa Nullish Coalescing Operator=====</p>`);
if (data === undefined || data === null) {
data = "nilai default";
}
document.writeln(`<p>${data}</p>`);
document.writeln(`<p>=====Dengan Nullish Coalescing Operator=====</p>`);
// data = parameter ? ? "Nilai Default";
document.writeln(`<p>${data}</p>`);
}
// ========== Optional Chaining ==========
{
document.writeln(`<p>=====Tanpa Oprional Chaining=====</p>`);
const person = {
address: {
country: "Indonesia",
},
};
}
// let country = person ? .address ? .country;
// document.writeln(`<p>${country}</p>`);
// ========== Falsy dan Truthy ==========
{
document.writeln(`<p>=====Tanpa Oprional Chaining=====</p>`);
let data = undefined;
if (data) {
document.writeln("TRUE");
} else {
document.writeln("FALSE");
}
}
// ========== Operator Logika di Non Boolean ==========
{
console.info("hello" || ""); // hello
console.info("" || []); // []
console.info("0" || "NOL"); // "0"
console.info(0 || "NOL"); // "NOL"
console.info(null || "NULL"); // "NULL"
console.info(undefined || "UNDEFINED"); // UNDEFINED
const person = {
firstName: "Fauzan",
lastName: "Ahmad",
};
const name = person.firstName || person.lastName;
console.info(name);
console.info("================AND===============");
console.info("hello" && ""); // ""
console.info("" && []); // ""
console.info("0" && "NOL"); // NOL
console.info(0 && "NOL"); // 0
console.info(null && "NULL"); // null
console.info(undefined && "UNDEFINED"); // undefined
console.info("undefined" && "null"); // "null"
}
// ========== For Loop ==========
// kode perulangan dengan kondisi
{
let counter = 1;
for (; counter <= 10;) {
document.writeln(`<p>Perulangan ke ${counter}</p>`);
counter++;
}
document.writeln(`<p>=======================</p>`);
// kode perulangan dengan init statement
for (let counter = 1; counter <= 10;) {
document.writeln(`<p>Perulangan ke ${counter}</p>`);
counter++;
}
document.writeln(`<p>=======================</p>`);
// kode perulangan dengan post statement
for (let counter = 1; counter <= 10; counter++) {
document.writeln(`<p>Perulangan ke ${counter}</p>`);
}
// ========== While Loop ==========
let counter = 10;
while (counter >= 1) {
document.writeln(`<p>Perulangan ke ${counter}</p>`);
counter--;
}
}
// ========== Do While Loop ==========
// ========== Break dan Continue ==========
{ // BREAK
let counter = 1;
while (true) {
document.writeln(`<p>Perulangan ke ${counter}</p>`);
counter++;
if (counter > 5) {
break;
}
}
document.writeln(`<p>Bilangan Ganjil Dengan Continue i % 2 === 0</p>`);
for (let i = 1; i <= 100; i++) {
if (i % 2 === 0) {
continue;
}
document.writeln(`<p>Perulangan Ganjil ke ${i}</p>`);
}
}
// ========== Label ==========
{ // kode : label
loopi: for (let i = 0; i < 100; i++) {
loopj: for (let j = 0; j < 10; j++) {
console.log(`${i}-${j}`);
}
}
// kode : Continue atau break di Label
loopa: for (let a = 0; a < 10; a++) {
loopb: for (let b = 0; b < 100; b++) {
if (b > 10) {
continue loopa;
}
document.writeln(`<p>${a}-${b}</p>`)
}
}
}
// ========== For In ==========
{ // Kode: For In di Object
const person = {
firstName: "Fauzan",
middleName: "Ahmad",
lastName: "M",
};
document.writeln(`<p>=====For In di Object=====</p>`);
for (const property in person) {
document.writeln(`<p>Property ${property} : ${person[property]}</p>`);
}
// Kode : For In di Array
document.writeln(`<p>=====For In di Array=====</p>`);
const names = ["Fauzan", "Tato", "Yono"];
for (const index in names) {
document.writeln(`<p>Index ${index} : ${names[index]}</p>`);
}
// ========== For Of ==========
document.writeln(`<p>==========Array Dengan For Of==========</p>`);
// Kode : For Of di Array
const names = ["Fauzan Ahmad", "Yono Kurniawan", "Wahyu Inna"];
for (const name of names) {
document.writeln(`<p>${name}</p>`);
}
document.writeln(`<p>==========String Dengan For Of==========</p>`);
// Kode : For Of di String
const name = "Fauzan Ahmad";
for (const char of name) {
document.writeln(`<p>${char}</p>`);
}
}
// ========== With Statement ==========
//Kode: Tanpa With Statement
{
document.writeln(`<div>Press F12 or klik inspect and console</div>`);
const person = {
firstName: "Fauzan",
lastName: "Ahmad",
};
console.log(`Tanpa With Statement`);
console.log(person.firstName);
console.log(person.lastName);
// Kode : Dengan With Statement
const human = {
firstName: "Fauzan",
lastName: "Ahmad",
};
console.log(`Dengan With Statement`);
with(human) {
console.log(firstName);
console.log(lastName);
}
// Kode : Ambigu di With Statement
console.log(`Ambigu di With Statement`);
const humanis = {
firstName: "Fatimah",
lastName: "Ahmad",
};
const firstName = "Bundo";
const lastName = "Nando";
with(humanis) {
console.log(firstName); // Niatnya ingin Bundo
console.log(lastName); // Niatnya ingin Nando
}
}
// ========== Function ==========
{
function sayHelloWorld() {
document.writeln(`<p>Hello World</p>`);
}
sayHelloWorld();
sayHelloWorld();
}
// ========== Function Parameter ==========
{ // Kode : Tanpa With Statement
document.writeln(`<div>Press F12 or klik inspect and console <br> to see the result</div>`);
function sayHello(firstName, lastName) {
console.log(`Hello ${firstName} ${lastName}`);
}
sayHello("Fauzan", "Racing");
}
// ========== Function Return Value ==========
{ // Kode : Function Return Value
document.writeln(`<div>Press F12 or klik inspect and console <br> to see the result</div>`);
function sayHello(firstName, lastName) {
return `Hello ${firstName} ${lastName}`;
}
console.log("====Return Value Satu====");
console.log(sayHello("Fauzan", "Racing"));
// Kode : Function Return Value Lebih Dari Satu
function getFinalScoreValue(value) {
if (value > 90) {
return "A";
} else if (value > 80) {
return "B";
} else if (value > 70) {
return "C";
} else if (value > 60) {
return "D";
} else {
return "E";
}
}
console.log("====Return Value Lebih Dari Satu====");
let inputNilai = 81;
let output = getFinalScoreValue(inputNilai);
console.log(output);
// Kode : Menghentikan Eksekusi dengan Return
function isContains(array, searchValue) {
for (const element of array) {
if (element === searchValue) {
return true;
}
}
return false;
}
console.log(isContains([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 5));
}
// ========== Optional Parameter ==========
// Kode : Kode optional parameter
{
document.writeln(`<div>Press F12 or klik inspect and console <br> to see the result</div>`);
function sayHello(firstName, middleName, lastName) {
console.log(firstName);
console.log(middleName);
console.log(lastName);
}
sayHello("Fauzan");
}
// ========== Default Parameter ==========
// Kode : Default Parameter
{
document.writeln(`<div>Press F12 or klik inspect and console <br> to see the result</div>`);
function register(name, gender = "UNKNOWN") {
console.log(nama);
console.log(gender);
}
register("Fauzan", "LAKIK");
register("Budi");
register("Joko", undefined);
}
// ========== Rest Parameter ==========
{ // Kode : Rest Parameter
function sum(name, ...data) {
let total = 0;
for (const item of data) {
total += item;
}
document.writeln(`<p>Total ${name} is ${total}</p>`);
}
sum("Mango", 50, 40, 3);
sum("Coconut", 50, 40, 3);
sum("Papaya", 50, 40, 3);
// Kode : Spread Syntax
const values = [10, 25, 34, 21, 22];
sum("Test", ...values);
// Kode : Arguments Object
document.writeln(`<p>=====Arguments Object (Old Sum)=====</p>`);
function oldSum() {
let total = 0;
for (const item of arguments) {
total += item;
}
document.writeln(`<p>Total is ${total}</p>`);
}
oldSum(1, 3, 2, 5, 2, 3);
}
// ========== Function Sebagai Value ==========
{ // Kode : Function di Variable
function sayHello(name) {
document.writeln(`<p>Hello ${name}</p>`);
}
document.writeln(`<p>=====Function di Variable=====</p>`);
let say = sayHello;
sayHello("Fauzan");
say("Tato");
// Kode : Function di Parameter
document.writeln(`<p>=====Function di Parameter=====</p>`);
function giveMeName(callback) {
callback("Fauzan Ahmad"); // sayHello("Fauzan Ahmad");
}
giveMeName(sayHello);
giveMeName(say); // giveMeName(sayHello)
}
// ========== Anonymous Function ==========
{ // Kode : Anonymous Function di Variable
let say = function (name) {
console.log(`Hello ${name}`);
document.writeln(`<p>Hello ${name}</p>`);
}
say("Fauzan");
// Kode : Anonymous Function di Parameter
function giveMeName(callback, age) {
callback("Fauzan", age);
}
giveMeName(function (name, age) {
document.writeln(`<p>Hello ${name} I'm ${age} years old</p>`);
}, 20);
}
// ========== Function dalam Function ==========
{ // Kode : Function dalam Function
function outer() {
function inner() {
document.writeln(`<p>Inner<p>`);
}
inner();
inner();
}
outer();
inner(); // ERROR can not access inner function (inner is not defined)
}
// ========== Scope ==========
{ // =====Kode : Global Scope=====
document.writeln(`<p>=====Global Scope=====</p>`);
// Variable counter berada di Global Scope
let counter = 0;
// Function hitMe berada di Global Scope
function hitMe() {
// blok functionnya berada di local scope function hitMe
counter++; // We can access
}
hitMe();
hitMe();
document.writeln(`<p>${counter}</p>`);
// =====Kode : Local Scope=====
document.writeln(`<p>=====Local Scope=====</p>`);
function first() {
let firstVariable = "First";
}
function secound() {
let secoundVariable = "Secound";
}
first();
secound();
//console.log(firstVariable); // ERROR can not access local scope
//console.log(secoundVariable); // ERROR can not access local scope
// =====Kode : Nested Function Scope=====
document.writeln(`<p>=====Nested Function Scope=====</p>`);
function firstNested() {
let firstVariable = "First";
function secoundNested() {
document.writeln(`<p>${firstVariable}</p>`);
}
secoundNested();
}
firstNested();
}
// ========== Recursive Function ==========
{ // =====Kode : Factorial Loop=====
document.writeln(`<p>=====Factorial dengan Loop=====</p>`);
function factorial(value) {
let result = 1;
for (let i = 1; i <= value; i++) {
result *= i;
}
return result;
}
let factorialValue = factorial(2);
document.writeln(`<p>${factorialValue}</p>`);
// =====Kode : Factorial Recursive=====
document.writeln(`<p>=====Factorial dengan Function Recursive=====</p>`);
function factorialRecursive(value) {
if (value === 1) {
return 1;
} else {
return value * factorialRecursive(value - 1);
}
}
factorialValue = factorialRecursive(20);
/*
Cara kerja Function Recursive
5 * factorialRecursive(5 - 1)
5 * 4 * factorialRecursive(4 - 1)
5 * 4 * 3 * factorialRecursive(3 - 1)
5 * 4 * 3 * 2 * factorialRecursive(2 - 1)
5 * 4 * 3 * 2 * 1
*/
document.writeln(`<p>${factorialValue}</p>`);
}
// ========== Function GEnerator ==========
{ // =====Kode : Function Generator Sederhana=====
document.writeln(`<p>=====Function Generator Sederhana=====</p>`);
function* createNames() {
yield "Fauzan";
yield "dopping";
yield "Tato";
}
for (const name of createNames()) {
document.writeln(`<p>${name}</p>`);
}
// =====Kode : Function Generator Mulai Tidak Sederhana (Lazy Load)=====
document.writeln(`<p>=====Function Generator Mulai Tidak Sederhana (Lazy Load)=====</p>`);
function* createOddNumbers(num) {
for (let i = 1; i <= num; i++) {
if (i % 2 === 1) {
document.writeln(`<p>${`Yield ${i}`}</p>`);
yield i;
}
}
}
const numbers = createOddNumbers(20);
// for (const number of numbers) {
// document.writeln(`<p>${number}</p>`);
// }
// Iterasi Dengan Next().value; bawaan function Generator
document.writeln(`<p>${numbers.next().value}</p>`);
document.writeln(`<p>${numbers.next().value}</p>`);
document.writeln(`<p>=====Function Generator Mulai Tidak Sederhana (Eager Load)=====</p>`);
function createOddNumbersArray(num) {
const result = [];
for (let i = 1; i <= num; i++) {
if (i % 2 === 1) {
document.writeln(`<p>${`Yield ${i}`}</p>`);
result.push(i);
}
}
return result;
}
const numbersArray = createOddNumbersArray(20);
console.log(numbersArray)
for (const number of numbersArray) {
document.writeln(`<p>${number}</p>`);
}
}
// ========== Arrow Function ==========
{ // =====Kode : Membuat Arrow Function=====
document.writeln(`<p>=====Membuat Arrow Function=====</p>`);
const sayHello = (name) => {
document.writeln(`<p>Hello ${name}</p>`);
let i = 1;
while (true) {
document.writeln(`<p>Hello ${name}</p>`);
i++;
if (i > 10) {
break;
}
}
}
sayHello("Fauzan");
// =====Kode : Arrow Function Tanpa Block=====
document.writeln(`<p>=====Arrow Function Tanpa Block=====</p>`);
const sayHelloWB = (name) => document.writeln(`<p>Hello ${name}</p>`);
sayHelloWB("Fauzan Juga")
// =====Kode : Arrow Function Return Value=====
document.writeln(`<p>=====Arrow Function Return Value=====</p>`);
const sum = (a, b) => a + b;
document.writeln(`<p>${sum(50, 1)}</p>`)
// =====Kode : Arrow Function Tanpa Kurung Parameter=====
document.writeln(`<p>=====Arrow Function Tanpa Kurung Parameter=====</p>`);
const sayStory = text => document.writeln(`<p>Lo keren bisa sampai sini bung ${text}</p>`);
sayStory("Fauzan");