-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy path1-array.js
More file actions
executable file
·121 lines (81 loc) · 3.18 KB
/
1-array.js
File metadata and controls
executable file
·121 lines (81 loc) · 3.18 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
// Arrays to work on
var numbers = [1, 12, 4, 18, 9, 7, 11, 3, 101, 5, 6];
var strings = ["this", "is", "a", "collection", "of", "words"];
// Use the above two arrays and practice array methods
// Find largest number in numbers
var maxNum = Math.max(...numbers);
console.log(maxNum);
// Find longest string in strings
var lengthArray = [];
var num = 0;
var longestString = "";
strings.forEach(function(element) {
if (element.length > num) {
num = element.length;
longestString = element;
}
});
// Find all the even numbers
var evenArray = numbers.filter(element => element % 2 == 0);
console.log(evenArray); // [12, 4, 18, 6]
// Find all the odd numbers
var oddArray = numbers.filter(element => element % 2);
console.log(oddArray); // [1, 9, 7, 11, 3, 101, 5]
// Find all the words that contain 'is' use string method 'includes'
var containsIs = strings.filter(element => element.includes("is"));
console.log(containsIs);
// Find all the words that contain 'is' use string method 'indexOf'
var arr = [];
var containsIsss = strings.forEach(function(element) {
if (element.indexOf("is") != -1) {arr.push(element);}
});
arr; // ["this", is]
// Check if all the numbers in numbers array are divisible by three use array method (every)
var divisibleByThree = numbers.every(function(element) {element % 3 == 0});
divisibleByThree // false
// Sort Array from smallest to largest
var sortStr = strings.sort();
var numArray = numbers.sort(function (a, b) {return a-b;});
sortStr; // ["a", "collection", "is", "of", "swastik"]
numArray; // [1, 3, 4, 5, 6, 7, 9, 11, 12, 18, 101]
// Remove the last word in strings
strings.pop();
strings // ["this", "is", "a", "collection", "of"]
// Add a new word in the array
strings.push("newWord");
strings // ["this", "is", "a", "collection", "of", "newWord"]
// Remove the first word in the array
strings.shift();
strings // ["is", "a", "collection", "of", "newWord"]
// Place a new word at the start of the array use (upshift)
strings.unshift("Hello");
strings // ["Hello", "is", "a", "collection", "of", "newWord"]
// Make a subset of numbers array [18,9,7,11]
var subSet = numbers.slice(3, 7);
subSet; // [18, 9, 7, 11];
// Make a subset of strings array ['a','collection']
var subStr = strings.slice(3, 7);
subStr; // ["a", "collection"]
// Replace 12 & 18 with 1221 and 1881
numbers.splice(3, 1, 1881);
numbers.splice(1, 1, 1221);
numbers; // [1, 1221, 4, 1881, 9, 7, 11, 3, 101, 5, 6]
// Replace words with string in strings array
// Customers Array
var customers = [
{ firstname: "Joe", lastname: "Blogs" },
{ firstname: "John", lastname: "Smith" },
{ firstname: "Dave", lastname: "Jones" },
{ firstname: "Jack", lastname: "White" }
];
// Find all customers whose firstname starts with 'J'
var startWithJ = customers.filter(element => element.firstname.startsWith('J'));
startWithJ;
// Create new array with firstname and lastname
var nameArray = [];
var name = customers.forEach(function(element) {
nameArray.push(`${element.firstname} + ${element.lastname}`);
});
nameArray; // ["Joe Blogs", "John Smith", "Dave Jones", "Jack White"]
// Sort the array created above alphabetically
nameArray.sort(); // ["Dave Jones", "Jack White", "Joe Blogs", "John Smith"]