-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrays.js
More file actions
1239 lines (882 loc) · 34.7 KB
/
Arrays.js
File metadata and controls
1239 lines (882 loc) · 34.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// JavaScript Arrays
// JavaScript arrays are used to store multiple values in a single variable.
{
let topCars = ["BMW","AUDI","MARCEDES BENZ"];
topCars.push("FERARY"); // If you want to add something after the Array list created you can use ".push()"method
topCars[3] = "Lamborgini"; // If you want to change any list item from Array,you can use this "name[Array item index position]"method
topCars.pop(); // "In JS Arrays,'.pop()'method remove the last item from Array list."
topCars.shift(); // "In JS Arrays,'.shift()'method remove the first item from Array list "
console.log(topCars);
console.log(topCars.length); //The "length" property iS always one more than the highest array index,and we know array index start from 0(zero).
console.log(topCars[topCars.length -1]);
}
{
//We can also create an array,and then provide the elements.
const cars = [];
cars[0] = "Saab";
cars[1] = "Volvo";
cars[2] = "BMW";
console.log(cars);
}
{
//Accessing array elements.
const cars = ["Toyota","Marcedes Benz","Audi"];
console.log(cars[0]);
//Array indexes start with 09(zero).
}
{
//Changing an Array element
const cars = ["Toyota","Marcedes Benz","Audi"];
cars[0] = "Bmw"; //It's similar to creating an empty array then providing elements
console.log(cars);
}
{
//Accesssing the full array elements,we need to provide the array name to access the full array
const cars = ["Toyota","Marcedes Benz","Audi"];
console.log(cars);
}
{
/*Array Elements Can Be Objects:
// JavaScript variables can be objects. Arrays are special kinds of objects.
Because of this, you can have variables of different types in the same Array.
You can have objects in an Array. You can have functions in an Array. You can have arrays in an Array:*/
//Example:
{
const cars =[ "firstName: Md Niamul Hakim", "lastName: Shawn", myFunction()];
function myFunction() {
let topRanking = "Data Scientist";
//console.log(topRanking);
return "most valuable job in 2021,Data Scientist";
}
console.log(cars);
console.log(typeof cars);
}
/*Arrays are Objects:
//Arrays are a special type of objects. The typeof operator in JavaScript returns "object" for arrays.
But, JavaScript arrays are best described as arrays.
//Arrays use numbers to access its "elements". In this example, person[0] returns John:
const person = ["John", "Doe", 46];
//Objects use names to access its "members". In this example, person.firstName returns John:
const person = {firstName:"John", lastName:"Doe", age:46};*/
}
{
//Accessing the Last Array Element
//Already we know how to access the 1st array element.
//But if we want to access the last element dynamically we have to use "arrayName[arrayName.length -1]"
//Why '-1'?
/*As we know array indexes start from '0(zero)',and the length is one more than the array index,if we
substract(-){
total (array index - array length),
it will return (-1) for the last array element
}*/
const fruits = ["Banana","Orange", "Apple", "Mango"];
console.log(fruits[0]);
console.log(fruits[fruits.length -1]);
const cars = ["BMW", "AUDI", "MARCEDES BENZ"];
cars[3] ="Lamborgini";
console.log(cars);
console.log(cars[0]);
console.log(cars[cars.length -1]);
console.log(cars.length);
}
{
//Looping Array Elements
//The Safest way to loop through an array,is using a "for" loop:
const Cars = ["BMW", "MARCEDES BENZ", "AUDI", "LAMBORGINI", "ASTEN MARTIN"];
//let carsLength = Cars.length;
//Creating "for"loop
for (i = 0; i < Cars.length; i++){
console.log(i);
console.log(Cars[i]);
}
}
{
//Creating another "for"loop
const fruits = ["Mango", "Banana", "Jack Fruit", "Orange", "Apple", "Strawberry"];
for(f = 0; f < fruits.length; f++){
console.log(fruits[f]);
}
}
{
//I can also use "arrayName.forEach()"function
const students =["Shawn", "Shain", "Bappy"];
students.forEach(id);
function id(value) {
console.log(value);
}
}
//Again creating 'for' loop
{
const helloJs = ["Hi guys, I'm Shawn", "I'm studying CSE", "At 'SouthWest Forestry University'", "I hope,I'll be a successful programmer soon"];
let hLength = helloJs.length;
for(h = 0; h < hLength; h++){
console.log(h);
console.log(helloJs[h]);
}
}
//Creating "forEach" function
{
const hiJs =["And now,I'm learning JavaScript", "As soon as possible I'll learn it", "Then I'll start 20 big project to check my skills", "And after that,I'll learn NodeJs for backend development"];
hiJs.forEach(myFunction);
function myFunction(value) {
console.log(value);
}
}
{
//again creating "forEach" function
const popUp = ["Hello JS", "Welcome to JS lesson"];
popUp.forEach(popUP);
function popUP(value) {
console.log(value);
}
}
//'for' loop
{
const helloJS = ["What's up guys", "I'm feeling excited to know what'll your next step"];
let hLength = helloJS.length;
for(h = 0; h < hLength; h++){
console.log(helloJS[h]);
}
}
//'forEach' function
{
const hiJS = ["And what you want to do in future", "Exectly what's your goal?"];
hiJS.forEach(hiJs);
function hiJs(value) {
console.log(value);
}
}
//Accessing the last array Element
{
const helloYou = ["Hello", "JavaScript", 1];
helloYou.push("not a good way to access the last array element");
console.log(helloYou[helloYou.length -1]); // This's the dynamic way to access the last array element
//you can also access the last array element by using the highest array index
console.log(helloYou[2]); //but later, if anyone change the array element you can't access by this way,below giving you a example
console.log(helloYou);
console.log(helloYou.length);
}
//New element can also be added to an array using the length property:
//we can add as the last element of the array by using this property
{
const wow = ["Hi there", "I'm Shawn", "I'm 21 years old"];
wow[wow.length] = "I'm studying CSE at Southwest Forestry University";
console.log(wow);
{
// Creating more array for practicing this property
let helloMe = ["Nice to meet you", "Hope see you soon"];
helloMe[helloMe.length] = "Have a good day";
console.log(helloMe);
}
}
{
/* //Associative Arrays
Many programming languages support arrays with named indexes.
Arrays with named indexes are called associative arrays (or hashes).
JavaScript does not support arrays with named indexes.
In JavaScript, arrays always use numbered indexes. */
/* Example
const person = [];
person[0] = "John";
person[1] = "Doe";
person[2] = 46;
person.length; // Will return 3
person[0]; // Will return "John"
*/
}
{
// The Difference Between Arrays and Objects:
// 1. In JavaScript, arrays use numbered indexes.
// 2. In JavaScript, objects use named indexes.
// Note: Arrays are a special kind of objects, with numbered indexes.
}
{
/*When to Use Arrays. When to use Objects.
a). JavaScript does not support associative arrays.
b). You should use objects when you want the element names to be strings (text).
c). You should use arrays when you want the element names to be numbers. */
}
{
// How to Recognize an Array?
//Solution 1:
/// To solve this problem ECMAScript 5 defines a new method Array.isArray():
const fruits = ["Mango", "Banana", "Straberry"];
console.log(Array.isArray(fruits));
let person = ["Shawn", "Shain", "Revu"]; //This one is array
let information = { //This one is object
person1Age: 21,
person2Age: 24,
person3Profession: function(){
console.log("Hi there");
}
};
console.log(Array.isArray(person));
console.log(Array.isArray(information));
information.person3Profession();
}
{
// Solution 2:
//The instanceof operator returns true if an object is created by a given constructor:
const fruits = ["Apple", "Banana", "Mango", "Pinapple"];
console.log(fruits instanceof Array);
}
// JavaScript Array Methods
{
//converting 'Arrays' to Strings
//The JavaScript method "toString()" converts an array to a string of (comma separated) array values.
const fruits = ["Banana", "Apple", "Mango", "Orange"];
console.log(fruits.toString());
//We learned this "toString()" method before in number methods lesson
let helloString = ["function", "Arrays", "Events", "Strings"];
let convertString = helloString.toString();
console.log(convertString);
}
{
// The "join()" method
// The "join()" method also joins all array elements into a string
// It behaves just like "toString()",but in addition you can specify the separator
const joinMethod = ["Apple", "Banana", "Orange"];
console.log(joinMethod.join());
//Creating more variables for practicing
let moreArrayConverting =["I'm 21 years old", "Just focusing on my goal", "I have no time to think about marriage"];
let convertingtoString = moreArrayConverting.join();
console.log(convertingtoString);
//creating "for" loop
for(m = 0; m < moreArrayConverting.length; m++){
console.log(moreArrayConverting[m]);
}
//creating "forEach" function
moreArrayConverting.forEach(converting);
function converting(value) {
console.log(value);
}
let helloJs = ["Hello Js", "Hello Python", "Hello Java", "Hello C", "Hello C++", "Hello Php"];
console.log(helloJs.join("*")); // We can also use any operator sign or anything to separete,if we don't want to use comma.
//One more example
let nowGoForFun = ["I'm Shawn ", " I don't follow any roles"];
console.log(nowGoForFun.join()); //if we don't use anything,then JS autometically provide comma to separate the String elements
console.log(nowGoForFun.join("/")); // Now I used division sign that's why it provide division sign
}
{
/*// Poping and Pushing
When you work with arrays, it is easy to remove elements and add new elements.
This is what popping and pushing is:
/// 'Popping' items out of an array, or 'pushing' items into an array.*/
}
{
//Popping
//The "pop()" method removes the last element from an array:
const fruits = ["Banana", "Orange", "Apple", "Mango"];
console.log(fruits.pop()); // It'll return the last array element 'Mango'
//If we want to check the full array now,there will be "Mango" missing,because we "popped out" that.
console.log(fruits);
let person = ["Shawn", "Shain", "Unknown"];
let personPop = person.pop(); // It'll return "Unknown", Because The pop() method returns the value that was "popped out":
console.log(person);
console.log(personPop);
}
{
// Pushing
// The "push()" method adds a new element to an array (at the end):
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.push("Kiwi");
console.log(fruits);
console.log(fruits.push()); //The push() method returns the new array length:
let person = ["Shawn", "Shain", "Unknown"];
person.push("Revu");
console.log(person);
console.log(person.push()); //The push() method returns the new array length:
}
{
let x = ["Hello everyone", "what's up"];
console.log(x.pop()); //"Pop()"method remove the last element from array.
console.log(x.shift()); //"shift()" method remove tha first element from array.
// one more array creating for practice 'shift()' method
const hiJs = ["20 years", "Shawn" , 21, "years old Shain"];
hiJs.shift();
console.log(hiJs);
console.log(hiJs.shift()); // The shift() method returns the value that was "shifted out"
}
{
/* Changing Elements
Array elements are accessed using their index number:
Array indexes start with 0:
[0] is the first array element
[1] is the second
[2] is the third ...*/
const fruits = ["Banana", "Orange", "Apple", "Mango"]; // Changes the first element of fruits to "Kiwi"
fruits[0] ="Kiwi";
console.log(fruits);
let person = ["Shawn", "Shain", "Unknown"]; //Change the last element of person to "Zahin"
person[2] = "Zahin";
console.log(person);
}
{
// Deleting Elements
//Since JavaScript arrays are objects, elements can be deleted by using the JavaScript operator delete:
// Using delete may leave undefined holes in the array. Use pop() or shift() instead.
const fruits = ["Banana", "Orange", "Apple", "Mango"];
delete fruits[0];
console.log(fruits);
console.log(fruits[0]); //So we should avoid "delete"operator,because it creates empty holes in array
}
{
//Splicing an Array
//The "splice()" method can be used to add new items to an array
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2, 0, "Strawberry","Kiwi");
console.log(fruits);
// The first parameter (2) defines the position 'where' new elements should be "added"(spliced in)
// The second parameter (0) defines 'how many' elements should be removed
}
{
// The "splice()" method returns an array with the deleted items
const fruits = ["Banana", "Orange", "Apple", "Mango"];
//fruits.splice(2, 2, "Pinapple", "Strawberry");
//console.log(fruits);
console.log(fruits.splice(2, 2, "Pinapple", "Strawberry"));
}
{
//Using splice() to Remove Elements
//With clever parameter setting, you can use splice() to remove elements without leaving "holes" in the array:
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(0, 1);
console.log(fruits);
}
// Merging (Concatenating) Arrays
{
//The concat() method creates a new array by merging (concatenating) existing arrays:
const myGirls = ["Cecilie", "Lone"];
const myBoys = ["Emil", "Tobias", "Linus"];
console.log(myGirls.concat(myBoys));
//The "concat()"method work like addition operator in JS
//we can also do this by using (+)operator
//console.log(myGirls + " " + myBoys);
// But this will returns string, not an array, thats the problem
const arr1 = ["Cecilie", "Lone"];
const arr2 = ["Emil", "Tobias", "Linus"];
const arr3 = ["Robin", "Morgan"];
console.log(arr1.concat(arr2, arr3));
}
//"slice()"method
{
//Slicing an Array
// The slice() method slices out a piece of an array into a new array.
//The slice() method creates a new array. It does not remove any elements from the source array.
const fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
const sliceFruits = fruits.slice(1);
const halfFruits = fruits.slice(1,4); //we can use two parameter to indicating start and ending point,but if we use one parameter it'll be okay,that's mean it will returns whole part after starting point
console.log(sliceFruits);
console.log(halfFruits);
}
//JavaScript Array Sort
{
//JavaScript Sorting Arrays
{
//Sorting an Array
//The ".sort()" method sorts an array alphabetically
const fruits = ["Banana", "Orange", "Apple", "Mango"];
console.log(fruits.sort()); //It'll returns(alphabetically) [ 'Apple', 'Banana', 'Mango', 'Orange' ]
//Reversing an Array
//The reverse() method reverses the elements in an array.
//You can use it to sort an array in descending order:
console.log(fruits.reverse()); //It'll returns descending order[ 'Orange', 'Mango', 'Banana', 'Apple' ]
}
//Numeric Sort
//By default, the sort() function sorts values as strings.
//However, if numbers are sorted as strings, "25" is bigger than "100", because "2" is bigger than "1".
//Because of this, the sort() method will produce incorrect result when sorting numbers.
//We can fix this by providing a compare function:
{
const points = [40, 100, 20, 60,80];
let x = points.sort(function(a,b) {
return a - b;
});
console.log(x); //This one will returns the ascending order
//Now have a look the descending order
let y = points.sort(function(c,d) {
return d - c;
});
console.log(y);
}
//The Compare Function:
{
//The purpose of the compare function is to define an alternative sort order.
//The compare function should return a negative, zero, or positive value, depending on the arguments:
//function(a, b){return a - b}
//When the sort() function compares two values, it sends the values to the compare function, and sorts the values according to the returned (negative, zero, positive) value.
/*
If the result is negative a is sorted before b.
If the result is positive b is sorted before a.
If the result is 0 no changes are done with the sort order of the two values.
*/
/*
Example:
The compare function compares all the values in the array, two values at a time (a, b).
When comparing 40 and 100, the sort() method calls the compare function(40, 100).
The function calculates 40 - 100 (a - b), and since the result is negative (-60), the sort function will sort 40 as a value lower than 100.
*/
}
//Sorting an Array in Random Order:
{
const points = [40, 100, 1, 5, 25, 10];
let x = points.sort(function(a,b) {
return 0.5 - Math.random();
});
console.log(x); //This order is not accurate,it will favor some numbers over the others
//The most popular correct method, is called the Fisher Yates shuffle, and was introduced in data science as early as 1938!
//We will learn it later,because it'll be so hard for us now.
}
//Again,sorting an Array in randoe order
{
let subjectMarks = [90, 70, 80, 75, 95];
let x = subjectMarks.sort(function(a,b) {
return 0.5 - Math.random();
});
console.log(x); //So after 2 example it's clear to all,this format is not correct,
}
//Find the Highest (or Lowest) Array Value:
{
//There are no built-in functions for finding the max or min value in an array.
//However, after you have sorted an array, you can use the index to obtain the highest and lowest values.
{
//Sorting ascending:
const points = [40, 100, 1, 5, 25, 10];
let x = points.sort(function(a, b) {
return a - b;
});
console.log(x);
console.log(x[0]); //It'll returns the lowest values
//console.log(x[5]); //This one not a proper way to get the last element from an Array,we have to do it dynamically
console.log(x[x.length -1]);//This one is the dynamic way to access the last element from an Array.
//Because if anyone change the array list later,there will be no problem,always we'll get the last element by this way
//Shorting descending order:
let y = points.sort(function(a, b) {
return b - a;
});
console.log(y);
console.log(y[0]); //Getting the Highest value in descending order
console.log(y[y.length -1]); //Getting the lowest value in descending order.
}
//Sorting Object Arrays:
//JavaScipt arrays often contain objects
{
const cars = [
{type:"Volvo", year:2016},
{type:"Saab", year:2001},
{type:"BMW", year:2010}
];
console.log(cars.sort(function(a, b) {
return a.year - b.year;
}));
}
//sorting object alphabitically
{
const cars = [
{type:"Volvo", year:2016},
{type:"Saab", year:2001},
{type:"BMW", year:2010}
];
console.log(cars.sort(function(a,b) {
let x = a.type.toLowerCase();
let y = b.type.toLowerCase();
if (x < y){return -1};
if (x>y){return 1};
return 0;
}));
}
{
const cars = [
{type:"Lamborgini", year:2016},
{type:"Audi", year:2010},
{type:"BMW", year:2001}
];
console.log(cars.sort(function(a,b){
return a.year - b.year;
}));
}
{
const cars = [
{type:"Lamborgini", year:2016},
{type:"Audi", year:2010},
{type:"BMW", year:2001}
];
console.log(cars.sort(function(a,b){
let x = a.type.toLowerCase();
let y = b.type.toLowerCase();
if (x < y){return -1};
if (x > y){return 1};
return 0;
}));
}
{
const resultsOf = [
{name:"Zahin got in this semester ", marks: 82, conslusinon:" numbers in Math."},
{name:"Shawn got in this semester ", marks: 96, conslusinon:" numbers in Math."},
{name:"Ashik got in this semester ", marks: 91, conslusinon:" numbers in Web Math."}
];
console.log(resultsOf.sort(function(a,b){
return a.marks - b.marks;
}));
console.log(resultsOf.sort(function(a,b){
let x = a.name.toLowerCase();
let y = b.name.toLowerCase();
if (x < y){return -1};
if (x > y){return 1};
return 0;
}));
}
}
// {
//forEach()method,just trying to remember not for any purpose
// const fruitsForEat = ["Unknown_1 body weight is 60kg", "Unkown_2 body weight is 65kg", "Unknown_3 body weight is 75kg"];
// fruitsForEat.forEach(amrMatha);
// function amrMatha(value){
// console.log(value);
// }
// }
{
//Arrays sorting
{
//using "sort()"method
const fruits = ["Banana", "Apple", "Orange", "Kiwi", "Strawberry"];
let fruitSort = fruits.sort();
console.log(fruits);
}
{
//Reverse sorting
const fruits = ["Banana", "Apple", "Orange", "Kiwi", "Strawberry"];
let fruitSort = fruits.sort();
let fruitSortReverse = fruits.reverse();
console.log(fruits);
}
//'.sort()'method works for letters correctly,
//but it doesn't work for numbers
//so we have to provide compare function inside sort function
{
//sorting array numbers in Ascending order:
const numbers = [100,10,80,50,30];
let ascendingNumbers = numbers.sort(function(a,b){
return a - b;
});
console.log(numbers);
}
{
//sorting array numbers in descending order
const numbers = [100,10,80,50,30];
let descendingNumbers = numbers.sort(function(a,b){
return b - a;
});
console.log(numbers);
}
//Sorting an Array in Random Order:
{
const numbers = [100,10,80,50,30];
let xRandomOrder =numbers.sort(function(a,b){
return 0.5 - Math.random();
});
console.log(numbers); //This will produce incorrect order,that's why sorting in random order not popular in js,most of people ignore this formet
//Again Random order sorting
const number = [200,1000,8,500,30];
let y = number.sort(function(a,b){
return 0.5 -Math.random();
});
console.log(number);
}
{
//Find the Highest (or Lowest) Array Value
//There are no built-in functions for finding the max or min value in an array.
//However, after you have sorted an array, you can use the index to obtain the highest and lowest values.
const points = [140, 100, 10, 5, 25, 110];
let x = points.sort(function(a,b){
return a - b;
});
console.log(points); // sorting in ascending order
console.log(points[0]); //getting the lowest value in this array
console.log(points[points.length -1]); // getting the highest value in this array using array index access property
}
{
//Sorting Object Arrays according to number
const cars = [
{type:"Volvo", year:2016},
{type:"Saab", year:2001},
{type:"BMW", year:2010}
];
let carSorting = cars.sort(function(a,b){
return a.year - b.year;
});
console.log(cars);
console.log(Array.isArray(cars)); //This will returns'true' because this is an array, but if we try to figure out this by using 'typeof'operator,it'll return object
console.log(cars instanceof Array); //This also return 'true',same as above comment
console.log(typeof cars); //This will return the result as 'object',but actually we know this one is an array and in JS every Array is special type of object
}
{
//Sorting Object Arrays according to word
const cars = [
{type:"Volvo", year:2016},
{type:"Saab", year:2001},
{type:"BMW", year:2010}
];
let xOfNumber = cars.sort(function(a,b){
let x = a.type.toLowerCase();
let y = b.type.toLowerCase();
if (x < y){return -1};
if (x > y){return 1};
});
console.log(cars);
}
}
}
//JavaScript Array Iteration
//Array iteration methods operate on every array item.
{
//Array.forEach()
//The "forEach()"method calls a function (a callback function)once for each array element
const numbers = [45, 4, 9, 16, 25];
numbers.forEach(myFan);
function myFan(value,index,array){
console.log(value);
console.log(index);
console.log(array);
}
const points = [100,10,40,70];
points.forEach(pointPass);
function pointPass(value,index,array){
console.log(value);
console.log(index);
console.log(array);
};
//Array.map() method
//The map() method creates a new array by performing a function on each array element.
{
//The map() method does not change the original array.
const ageofStudents = [21, 23, 24];
let doubleAge = ageofStudents.map(whatEver);
function whatEver(value,index,array){
return value * 2;
}
console.log(doubleAge);
}
{
const halfAgeFromCurrentAge = [10.5, 11.5, 13];
let currentAge = halfAgeFromCurrentAge.map(realAge);
function realAge(value,index,array){
return value * 2;
};
console.log(currentAge);
}
//Array.filter()
{
//The filter() method creates a new array with array elements that passes a test.
{
const numbers = [45, 4, 9, 16, 25];
let newNumbers = numbers.filter(nanOfAll);
function nanOfAll(value,index,array){
return value > 18;
};
console.log(newNumbers);
}
{
const points = [60,100,50,10,5];
let filPoints = points.filter(fillingPo);
function fillingPo(value,index,array){
return value > 10;
};
console.log(filPoints);
}
}
//Array.reduce() method
{
//The reduce() method runs a function on each array element to produce (reduce it to) a single value.
//The reduce() method works from left-to-right in the array. See also reduceRight().
//The 'reduce()' method does not reduce the original array.
const numbers = [45, 4, 9, 16, 25];
let nBrs = numbers.reduce(nTdots);
function nTdots(total,value,index,array){
return total + value;
};
console.log(numbers); ////The 'reduce()' method does not reduce the original array.
console.log(nBrs);
/*
Note that the function takes 4 arguments:
1.)The total (the initial value / previously returned value)
2.)The item value
3.)The item index
4.)The array itself
The example above does not use the index and array parameters. It can be rewritten to:
*/
/*
const numbers = [45, 4, 9, 16, 25];