-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
1330 lines (1066 loc) · 29 KB
/
index.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
//Comment
console.log("Hello World");
//Best Practices you have to declare every variable in different line
let nameOne = "Arturo";
let lastNameOne = "Lira";
console.log(nameOne + " " + lastNameOne);
let firstName = "ArturoL";
console.log(firstName);
const interestRate = 0.3;
//interestRate =1; Bad Practices
console.log(interestRate);
/*******PRIMITIVE TYPES ******/
/**
* String
* Number
* Boolean
* Undefined
* null
*/
let name = "Arturo"; // String Literal
let age = 30; // Number Literal
let isApproved = false; // Boolean Literal
//let firstNames = undefined; // It is not necessary to define undefined variable
let lastNames = null;
let selectedColor = null;
selectedColor = "red";
/******VALUE TYPES ******/
/**
* Object
* Array
* Function
*/
/**OBJECT */
let person = { name: "Arturo", age: 38 }; //Object Literal
//Dot Notation
person.name = "Jose";
//Bracket Notation
let selection = "name";
//person['name'] = 'Juan';
person[selection] = "Josh";
console.log(person);
/**ARRAY */
let selectedColors = ["red", "blue"];
selectedColors[2] = "green";
selectedColors[3] = 3;
console.log(selectedColors);
console.log(selectedColors[0]);
console.log(selectedColors.length);
/**FUNCTION */
/**Performing a task */
function greet(name, lastName) {
console.log("Hello World inside of function declaration");
console.log("Hello " + name + " " + lastName);
}
greet("Arturo", "Lira");
/**Calculating a value */
function square(number) {
return number * number;
}
let number = square(2);
console.log(square(2));
/**OPERATOR **/
/**
* Arithmetic
* Assignment
* Comparison
* Logical
* Bitwise
*/
/**Arithmetic */
let x = 10;
let y = 3;
// console.log(x + y); /**addition */
// console.log(x - y); /**Subtraction */
// console.log(x * y); /**Multiplication */
// console.log(x / y); /**Division */
// console.log(x % y); /**Remainder of Division */
// console.log(x ** y); /**Exponentiation */
//Increment (++)
console.log(++x);
console.log(x++);
//Decrement (--);
/*console.log(--x);
console.log(x--);*/
/**Assignment operator */
/**Exercises */
/**FizzBuzz*/
//Divisible by 3 ==> Fizz
//Divisible by 5 ==> Buzz
//Divisible by both 3 and 5 ==>FizzBuzz
//Not divisible by 3 or 5 ==> input
//Not a number ==> 'Not a number'
const output = fizzBuzz("value");
console.log(output);
function fizzBuzz(input) {
if (typeof input !== "number") return NaN;
if (input % 3 === 0 && input % 5 === 0) {
return "FizzBuzz";
} else if (input % 3 === 0) {
return "Fizz";
} else if (input % 5 === 0) {
return "Buzz";
} else return input;
}
// /***CHECK SPEED */
//Speed Limit = 70
checkSpeed(135);
function checkSpeed(speed) {
const SpeedLimit = 70;
const kmPerPoint = 5;
if (speed < SpeedLimit + kmPerPoint) console.log("Ok");
else {
const Points = Math.floor((speed - SpeedLimit) / kmPerPoint);
if (Points > 12) console.log("LICENSE SUSPENDED");
else console.log("Points", Points);
}
}
// document.getElementById("a").innerHTML();
let NameOne = "Arturo".toUpperCase();
console.log(NameOne);
let txt = "I can eat bananas all day";
let result = txt.slice(10, 17);
console.log(result);
const fruits = ["Banana", "Orange", "Apple"];
fruits.pop();
fruits.push("Kiwi");
const d = new Date();
d.setFullYear(2001);
console.log(d);
let max = Math.max(10, 20);
let round = Math.round(5.3);
// document.getElementById("image").src = "pic_mountain.jpg";
// document.getElementsByTagName("p")[0].innerHTML = "Hello from JS";
// document.getElementById("myText").value = "Have a nice day!";
// document.getElementById("demo").style.color = "red";
// document.getElementById("demo1").style.fontSize = "70px";
// document.getElementById("demo1").style.display = "none";
function normalize(param) {
param.replace();
return console.log(param.replace("/-/", "/"));
}
normalize("20-05-2017");
console.log(Math.ceil(856.12));
function dice() {
return Math.ceil(Math.random() * 6);
}
console.log(dice());
function add(str) {
str.substr;
return parseInt(str, 10) + parseInt(str.substr(str.indexOf("+")), 10);
}
console.log(add("102+17"));
function getLastElement(vArray) {
let len = vArray.length;
return vArray[len];
}
console.log(getLastElement([1, 2]));
let abcd = ["a", "b", "c", "d"];
let ab = abcd.slice(0, 2);
let bc = abcd.slice(1, 3);
let bcd = abcd.slice(1, 4);
console.log(ab);
console.log(bc);
console.log(bcd);
function cutComment(value) {
if (value.indexOf("//") != -1) {
let comm = value.indexOf("//") + 2;
return value.slice(comm, value.length);
}
return null;
}
console.log(cutComment("let foo; // bar"));
function gcd(a, b) {
let numgcd = 0;
let restA = 0;
let restB = 0;
do {
numgcd++;
restA = numgcd % a;
restB = numgcd % b;
} while (!(restA >= 0 && restB >= 0));
return numgcd;
}
gcd(6, 15);
function maxi() {
let array = [arguments.length];
for (let i = 0; i < arguments.length; i++) {
array[i] = arguments[i];
}
return Math.max(...array);
}
maxi(1, 2);
maxi(2, 3, 1);
function parseFirstInt(value) {
let inputToParse = value;
for (let i = 0; i < value.length; i++) {
let firstInt = parseInt(inputToParse);
if (!Number.isNaN(firstInt)) {
return firstInt;
}
inputToParse = inputToParse.substr(1);
}
return NaN;
}
console.log(parseFirstInt("No. 10"));
console.log(parseFirstInt("Babylon"));
function sum(value) {
let sumArray = 0;
for (let i = 0; i < value.length; i++) {
sumArray += value[i];
}
return sumArray;
}
function mean(value) {
if (value.length == 0) {
return 0;
}
return sum(value) / value.length;
}
mean([0]);
function arabic(value) {
let valueGetArabic = value;
let romanValue = 0;
for (let i = 0; i < value.length; i++) {
switch (valueGetArabic[i]) {
case "I": {
if (valueGetArabic[i + 1] == "V") {
romanValue += 4;
i++;
continue;
} else if (valueGetArabic[i + 1] == "X") {
romanValue += 9;
i++;
continue;
}
romanValue += 1;
break;
}
case "V":
romanValue += 5;
break;
case "X": {
if (valueGetArabic[i + 1] == "L") {
romanValue += 40;
i++;
continue;
} else if (valueGetArabic[i + 1] == "C") {
romanValue += 90;
i++;
continue;
}
romanValue += 10;
break;
}
case "L":
romanValue += 50;
break;
case "C": {
if (valueGetArabic[i + 1] == "D") {
romanValue += 400;
i++;
continue;
} else if (valueGetArabic[i + 1] == "M") {
romanValue += 900;
i++;
continue;
}
romanValue += 100;
break;
}
case "D":
romanValue += 500;
break;
case "M":
romanValue += 1000;
break;
}
}
return romanValue;
}
arabic("CDLXXXIII");
arabic("X");
const roman = (num, result = "") => {
const map = {
M: 1000,
CM: 900,
D: 500,
CD: 400,
C: 100,
XC: 90,
L: 50,
XL: 40,
X: 10,
IX: 9,
V: 5,
IV: 4,
I: 1,
};
for (const key in map) {
if (num >= map[key]) {
if (num !== 0) {
return roman(num - map[key], result + key);
}
}
}
return result;
};
roman(483);
function sumMultiples(value) {
let sum = 0;
for (let i = 1; i < value; i++) {
if (i >= 3) {
if (i % 3 == 0 || i % 5 == 0) {
sum += i;
}
}
}
return sum;
}
sumMultiples(3);
//Digit Sum
function digitSum(value) {
let sum = 0;
let len = value.toString().length;
for (let i = 0; i < len; i++) {
sum += parseInt(value[0]);
}
return sum;
}
digitSum(1);
//Swapping Variables
let a = "red";
let b = "blue";
let c = "";
c = a;
a = b;
b = c;
console.log(a);
console.log(b);
//If Statement Conditions
/****
* Hour
* If hour is between 6 AM and 12 PM: Good morning!
* If It is between 12PM and 6PM : Good afternoon!
* Otherwise: Good evening!
*/
let hour = 19;
if (hour >= 6 && hour <= 12) console.log("Good morning");
else if (hour >= 12 && hour <= 18) console.log("Good afternoon");
else console.log("Good evening");
//Switch Statement
let role;
switch (role) {
case "guest":
console.log("Guest User");
break;
case "moderator":
console.log("Moderator User");
break;
default:
console.log("Unknown User");
}
//If Statement
if (role === "guest") console.log("Guest");
else if (role === "moderator") console.log("moderator");
else console.log("Unknown");
// for Loop Statement
for (let i = 1; i <= 5; i++) {
//console.log("Hello Word", i);
//Odd Numbers
if (i % 2 !== 0) console.log(i);
}
console.log("*****Odd Number reverse order*****");
//Odd Number reverse order
for (let i = 5; i >= 1; i--) {
if (i % 2 !== 0) console.log(i);
}
console.log("*****While statement*****");
//While statement
let i = 0;
while (i <= 5) {
if (i % 2 !== 0) console.log(i);
i++;
}
console.log("*****Do-While statement*****");
// Do-while statement occur at least once.
let iw = 0;
do {
if (iw % 2 !== 0) console.log(iw);
iw++;
} while (iw <= 5);
console.log("*****for-in statement*****");
//for-in
const personNew = {
name: "Arturo",
age: 39,
};
for (let key in personNew) console.log(key, personNew[key]);
const colors = ["red", "green", "blue"];
for (let index in colors) console.log(colors[index]);
// for-of
console.log("*****for-in statement*****");
for (let color of colors) console.log(color);
//Break and continue
console.log("*****Break and continue statement*****");
console.log("*****Break statement*****");
let variable = 0;
while (variable <= 10) {
if (variable === 5) break;
console.log(variable);
variable++;
}
console.log("*****Continue statement*****");
let variableDos = 0;
while (variableDos <= 10) {
if (variableDos % 2 === 0) {
// Get Even Numbers
variableDos++;
continue;
}
console.log(variableDos);
variableDos++;
}
/**
* Write a function that takes two numbers and returns
* the maximum of the two.
*/
console.log("*****function MaxTwoNumbers statement*****");
function maxTwoNumbers(numberUno, numberTwo) {
return numberUno > numberTwo ? numberUno : numberTwo;
}
console.log(maxTwoNumbers(25, 10));
/**
* Landscape function
*/
console.log("*****function isLandscape*****");
function isLandscape(width, height) {
return width > height;
}
console.log(isLandscape(11, 8.5));
/**
* FizzBuzz function
* Divisible by 3 => 'Fizz'
* Divisible by 5 => 'Buzz'
* Divisible by 3 and 5 => 'FizzBuzz'
* Not Divisible by 3 or 5 => input
* Not a Number => 'Not a Number'
*
*/
console.log("*****function FizzBuzz*****");
const outputTwo = fizzBuzzTwo(true);
console.log(outputTwo);
function fizzBuzzTwo(input) {
if (typeof input === "number") {
if (input % 3 === 0 && input % 5 === 0) return "FizzBuzz";
if (input % 3 === 0) return "Fizz";
if (input % 5 === 0) return "Buzz";
return input;
}
return NaN;
}
/**
* CheckSpeed function
* Speed Limit = 70 <= 'Ok'
* Every 5 km that you above of the limit
* You will get 1 point
* Math.floor(1.3) => 1
* More than 12 point 'License suspended'
*
*/
console.log("*****function CheckSpeed*****");
CheckSpeedTwo(130);
function CheckSpeedTwo(speed) {
const SpeedLimit = 70;
const kmPerPoint = 5;
if (speed < SpeedLimit + kmPerPoint) {
return console.log("OK");
}
const passLimit = speed - SpeedLimit;
const calculationPoints = Math.floor(passLimit / kmPerPoint);
if (calculationPoints >= 12) return console.log("License suspended");
else return console.log(calculationPoints + " " + "Point");
}
/**
* Show Number function
*/
console.log("*****function Show Number*****");
showNumber(10);
function showNumber(limit) {
for (let i = 0; i <= limit; i++) {
const message = i % 2 === 0 ? "EVEN" : "ODD";
console.log(i, message);
// if (i % 2 === 0) {
// console.log(i, "EVEN");
// } else console.log(i, "ODD");
}
}
/**
* countTruthy
*/
console.log("****** Count Truthy function*****");
console.log(countTruthy([0, 2, 3]));
function countTruthy(array) {
let count = 0;
for (let value of array) {
if (value) count += 1;
}
return count;
}
/**Show Properties
*
*/
console.log("*******Show Properties function******");
const movie = {
title: "a",
releaseYear: 2018,
rating: 4.5,
director: "b",
};
showProperties(movie);
function showProperties(obj) {
for (let key in obj)
if (typeof obj[key] === "string") console.log(key, obj[key]);
}
/**Sum function
*
*/
console.log("*******Sum function******");
console.log(sumTwo(10));
function sumTwo(value) {
let sum = 0;
for (let i = 0; i <= value; i++) if (i % 3 === 0 || i % 5 === 0) sum += i;
return sum;
}
/**Grades function
* First Calculate the Average Grade
* 1-59: F
* 60-69: D
* 70-79: C
* 80-89: B
* 90-100 : A
*/
console.log("*******Grade function******");
const marks = [80, 80, 50];
console.log(calculateGrade(marks));
function calculateGrade(marks) {
const average = calculateAverage(marks);
if (average < 60) return "F";
if (average < 70) return "D";
if (average < 80) return "C";
if (average < 90) return "B";
return "A";
}
function calculateAverage(array) {
let sum = 0;
for (let value in array) sum += marks[value];
return sum / marks.length;
}
/**
* function stars
*/
console.log("*****Show Stars******");
showStars(2);
function showStars(value) {
let start = "";
for (let i = 1; i <= value; i++) console.log((start += "*"));
}
/**
* function Show Primes
*/
console.log("*****Show Primes******");
showPrimes(20);
function showPrimes(limit) {
for (let number = 2; number <= limit; number++)
if (isPrime(number)) console.log(number);
}
function isPrime(number) {
for (let factor = 2; factor < number; factor++)
if (number % factor === 0) return false;
return true;
}
console.log("***** Arrays ********");
const numbers = [3, 4];
//End
numbers.push(5, 6);
console.log(numbers);
//Beginning
numbers.unshift(1, 2);
console.log(numbers);
//Middle
numbers.splice(2, 0, "a", "b");
console.log(numbers);
console.log("******Find Primitive types***");
/**Find Primitive types */
const numbersTwo = [1, 2, 3, 4];
console.log("Not Found Value indexOf(0) Strict Equality");
console.log(numbersTwo.indexOf(0));
console.log("Found Value indexOf(3) Strict Equality");
console.log("Index=>", numbersTwo.indexOf(3));
console.log("Found Value lastIndexOf(4) Strict Equality");
console.log("Index=>", numbersTwo.lastIndexOf(4));
console.log("Get true or false If Found Value indexOf(3) Strict Equality");
console.log("IsTheValue=>", numbersTwo.indexOf(3) !== -1);
console.log("Get true or false If Found Value includes(1) Strict Equality");
console.log("IsTheValue=>", numbersTwo.includes(1));
console.log("Second Parameter Found Value indexOf(1, 2) Strict Equality");
console.log("SecondeParameter=>", numbersTwo.indexOf(1, 2));
console.log("Second Parameter Found Value includes(1, 2) Strict Equality");
console.log("IsTheValue=>", numbersTwo.includes(1, 2));
console.log("Finding Reference Types");
const courses = [
{ id: 1, name: "Arturo" },
{ id: 2, name: "Jose" },
{ id: 3, name: "Josh" },
];
console.log('courses.find() Found Element course.name === "Arturo"');
console.log(
courses.find(function (course) {
return course.name === "Arturo";
})
);
console.log('courses.find() Not Found Element course.name === "A"');
console.log(
courses.find(function (course) {
return course.name === "A";
})
);
console.log('courses.findIndex() Found Element course.name === "Arturo"');
console.log(
"Index=>",
courses.findIndex(function (course) {
return course.name === "Arturo";
})
);
console.log('courses.findIndex() Not Found Element course.name === "A"');
console.log(
"Index=>",
courses.findIndex(function (course) {
return course.name === "A";
})
);
console.log("***********Arrow functions***********");
console.log('courses.find() Found Element course.name === "Arturo"');
console.log(
"object=>",
courses.find((course) => course.name === "Arturo")
);
console.log('courses.findIndex() Found Element course.name === "Arturo"');
console.log(
"Index=>",
courses.findIndex((course) => course.name === "Arturo")
);
console.log("***********Remove Element in ARRAYS****************");
//End
console.log("Remove from End Element of Array");
const last = numbersTwo.pop();
console.log("Element removed from the Array => ", last);
console.log(numbersTwo);
// Beginning
console.log("Remove from Beginning Element of Array");
const first = numbersTwo.shift();
console.log("Element removed from the Array => ", first);
console.log(numbersTwo);
//Middle
const numbersThree = [1, 2, 3, 4];
console.log("Remove from Middle Element of Array");
const middle = numbersThree.splice(2, 1);
console.log("Element removed from the Array => ", middle);
console.log(numbersThree);
console.log("***********Emptying ARRAYS****************");
let emptedArray = [1, 2, 3, 4, 5];
//Reference
let another = emptedArray;
console.log("********Solution 1********");
emptedArray = [];
console.log(
"This does not work with multiple reference to emptedArray".toUpperCase()
);
console.log("New Empted Array =>", emptedArray);
console.log("Reference Another Array =>", another);
console.log("********Solution 2********");
let solutionTwo = [1, 2, 3, 4, 5];
console.log("Solution 2 Initial Array 'solutionTwo'", solutionTwo);
console.log("Solution 2 solutionTwo.length = 0");
solutionTwo.length = 0;
console.log("Solution 2", solutionTwo);
console.log("********Solution 3********");
let solutionThree = [1, 2, 3, 4, 5];
console.log("Solution 3 Initial Array 'solutionThree'", solutionThree);
solutionThree.splice(0, solutionThree.length);
console.log("Solution 3 Empty 'solutionThree' solutionThree.splice = 0");
console.log("Solution 3 solutionTwo.splice = 0");
console.log("Solution 3", solutionThree);
console.log("********Solution 4********");
let solutionFour = [1, 2, 3, 4, 5];
console.log("Solution 4 Initial Array 'solutionFour'", solutionFour);
while (solutionFour.length > 0) solutionFour.pop();
console.log("Solution 4 Empty 'solutionFour' solutionFour.pop() in Loop");
console.log("Solution 4 solutionFour.pop()");
console.log("Solution 4", solutionFour);
console.log("********Combine********");
const firstArray = [1, 2, 3];
const secondArray = [4, 5, 6];
const resultArray = firstArray.concat(secondArray);
console.log("Function", "firstArray.concat(secondArray)");
console.log(resultArray);
const slice = resultArray.slice(2, 4);
console.log("Function", "resultArray.slice(2,4)");
console.log(slice);
console.log("*********Spread Operator*********");
const combine = [...firstArray, "a", ...secondArray, "b"];
console.log("[...firstArray, ...secondArray]");
console.log(combine);
console.log("********Copy with Slice()*****");
console.log(combine.slice());
console.log("********Copy with Spread Operaor ...*****");
console.log([...combine]);
console.log("********Iterating an Array*****");
const num = [1, 2, 3];
console.log("for (let number of num)");
for (let number of num) console.log(number);
console.log("foreach num.forEach(callback)");
num.forEach(function (number) {
console.log(number);
});
console.log(" num.forEach Arrow callback function");
num.forEach((number) => console.log(number));
console.log(" num.forEach Arrow callback function with two parameters");
num.forEach((number, index) =>
console.log("index=>", index, "value=>", number)
);
console.log("********Joining an Array*****");
const numbersJoin = [1, 2, 3, 4];
const join = numbersJoin.join(",");
console.log(" numbersJoin.join(',') function");
console.log(join);
console.log("function string split message.split('') convert in array");
const message = "This is my first message";
const split = message.split(" ");
console.log(split);
console.log("Combine function to join split.join('-') ");
console.log(split.join("-"));
console.log("********function sort() in array primitive values********");
const numbersSort = [1, 3, 2, 4];
console.log("numbersSort Inital values", numbersSort);
console.log("numbersSort.sort()");
console.log(numbersSort.sort());
console.log("********function sort() in array references values********");
const coursesSort = [
{ id: 1, name: "Node.js" },
{ id: 2, name: "javaScript" },
];
console.log(coursesSort);
coursesSort.sort(function (a, b) {
// a<b => -1
// a>b => 1
// a===b =>0
const nameA = a.name.toUpperCase();
const nameB = b.name.toUpperCase();
if (nameA < nameB) return -1;
if (nameA > nameB) return 1;
return 0;
});
console.log(coursesSort);
const numbersEvery = [1, 3, 2, 4];
console.log("********function every Array********");
console.log(
numbersEvery.every(function (value) {
return value >= 0;
})
);
const numbersSome = [-1, 3, 2, 4];
console.log("********function every Array********");
console.log(
numbersSome.some(function (value) {
return value >= 0;
})
);
const numbersFilter = [-1, 3, 2, 4];
console.log("********function filter Array********");
const filtered = numbersFilter.filter(function (number) {
return number >= 0;
});
console.log(filtered);
console.log("********function filter Array********");
const filteredArrow = numbersFilter.filter((number) => number >= 0);
console.log(filteredArrow);
console.log("********function map Array********");
const mapfilter = [1, 2, 3];
const filtering = mapfilter.map((number) => "<li>" + number + "</li>");
console.log(filtering);
const html = "<ul>" + filtering.join(" ") + "</ul>";
console.log(html);
const item = filtered.map((n) => ({ value: n }));
console.log(item);
console.log("chain the methods");
const chain = numbersFilter
.filter((number) => number >= 0)
.map((n) => ({ value: n }));
console.log(chain);
console.log("********function Reduce Array********");
console.log("Normal sum using for (let)");