-
Notifications
You must be signed in to change notification settings - Fork 0
/
Array_Methods.js
58 lines (50 loc) · 1.71 KB
/
Array_Methods.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
var New_Array = [143, "I Love you", "God", 21, 29, 2004, 2002, true, false];
// These 2 functions make my life easier
function line() {
console.log("----------------------------------");
}
function print(x) {
console.log(x);
}
// For of method
print("For of Method: \n");
for (item of New_Array) {
// Returns the array value
print(item);
}
line();
// Map Method
print("Map Method: \n")
New_Array.map((item, index)=> {
// Returns the index and value of the array
print("Array index is: " + index + " and the value is: " + item);
});
line();
// For Each Method
print("For Each Method: \n")
New_Array.forEach(function(item,index) {
// Returns the index and value of the array
print("Array index is: " + index + " and the value is: " + item);
});
line();
// Every Method
print("Every Method: \n");
let everyMethod = New_Array.every(item => !isNaN(item)); // Test if every element in the array is a number, returns boolean value only
let everyMethodBool = everyMethod ? "Every element is a number" : "Not every element is a number";
print(everyMethodBool);
line();
// Some Method
print("Every Method: \n");
let someMethod = New_Array.some(item => !isNaN(item)); // Test if some element in the array is a number, returns boolean value only
let someMethodBool = someMethod ? "Some element is a number" : "There is no number element";
print(someMethodBool);
line();
// Filter Method
print("Filter Method: \n");
New_Array.filter((item, index) =>
print("Is array index " + index + " a number? --> " + !isNaN(item))
); // Return boolean values per array
// Reduce Method
print("Reduce Method: \n")
let concatenated = New_Array.reduce((accumulator, currentValue) => `${accumulator} ${currentValue}`);
print(concatenated);