1
+ // Declare an empty array;
2
+ const arr = [ ] ;
3
+ console . log ( arr ) ;
4
+
5
+ // Declare an array with more than 5 number of elements
6
+ const listOfItems = [ 'Pen' , 'Book' , 'Shoe' , 'Grapes' , 'Brush' ] ;
7
+ console . log ( listOfItems ) ;
8
+
9
+ // Find the length of your array
10
+ console . log ( listOfItems . length ) ;
11
+
12
+ // Get the first item, the middle item and the last item of the array
13
+ let firstItem = listOfItems [ 0 ] ;
14
+ console . log ( firstItem ) ;
15
+
16
+ let middleItem = listOfItems [ 2 ] ;
17
+ console . log ( middleItem ) ;
18
+
19
+ let lastIndex = listOfItems . length - 1 ;
20
+ console . log ( listOfItems [ lastIndex ] ) ;
21
+
22
+ /* Declare an array called mixedDataTypes, put different data types in
23
+ the array and find the length of the array. The array size should be greater than 5*/
24
+ let mixedDataTypes = [ 1 , 'David' , 'Ghanaian' , true , ] ;
25
+
26
+ // Declare an array variable name itCompanies and assign initial values Facebook, Google, Microsoft, Apple, IBM, Oracle and Amazon
27
+ const itCompanies = [ 'Facebook' , 'Google' , 'Microsoft' , 'Apple' , 'IBM' , 'Oracle' , 'Amazon' ] ;
28
+ console . log ( itCompanies ) ;
29
+ console . log ( itCompanies . length ) ;
30
+
31
+ // Print the first company, middle and last company
32
+ let firstCompany = itCompanies [ 0 ] ;
33
+ console . log ( firstCompany ) ;
34
+
35
+ let middleCompany = itCompanies [ 3 ] ;
36
+ console . log ( middleCompany ) ;
37
+
38
+ lastIndex = itCompanies . length - 1 ;
39
+ console . log ( itCompanies [ lastIndex ] ) ;
40
+
41
+ // Change each company name to uppercase one by one and print them out
42
+ let fb = itCompanies [ 0 ] ;
43
+ console . log ( fb . toUpperCase ( ) ) ;
44
+ let ggle = itCompanies [ 1 ] ;
45
+ console . log ( ggle . toUpperCase ( ) ) ;
46
+ let ms = itCompanies [ 2 ] ;
47
+ console . log ( ms . toUpperCase ( ) ) ;
48
+ let apple = itCompanies [ 3 ] ;
49
+ console . log ( apple . toUpperCase ( ) ) ;
50
+ let ibm = itCompanies [ 4 ] ;
51
+ console . log ( ibm . toUpperCase ( ) ) ;
52
+ let oracle = itCompanies [ 5 ] ;
53
+ console . log ( oracle . toUpperCase ( ) ) ;
54
+ let amazon = itCompanies [ 6 ] ;
55
+ console . log ( amazon . toUpperCase ( ) ) ;
56
+
57
+ // Print the array like as a sentence: Facebook, Google, Microsoft, Apple, IBM,Oracle and Amazon are big IT companies.
58
+ console . log ( itCompanies . join ( ', ' ) , 'are big IT companies' ) ;
59
+
60
+ // Check if a certain company exists in the itCompanies array. If it exist return the company else return a company is not found
61
+ let exits = itCompanies . indexOf ( 'Google' ) ;
62
+ if ( exits === 1 ) {
63
+ console . log ( itCompanies [ 1 ] ) ;
64
+ }
65
+ else {
66
+ console . log ( 'Company is not found' ) ;
67
+ }
68
+
69
+ // Sort the array using sort() method
70
+ console . log ( itCompanies . sort ( ) ) ;
71
+
72
+ // Reverse the array using reverse() method
73
+ console . log ( itCompanies . reverse ( ) ) ;
74
+
75
+ // Slice out the first 3 companies from the array
76
+ console . log ( itCompanies . slice ( 3 , 7 ) ) ;
77
+
78
+ // Slice out the last 3 companies from the array
79
+ console . log ( itCompanies . slice ( 0 , 4 ) ) ;
80
+
81
+ console . log ( itCompanies ) ;
82
+ // Remove the first IT company from the array
83
+ console . log ( itCompanies . splice ( 0 , 1 ) ) ;
84
+
85
+ // Remove the middle IT company or companies from the array
86
+ console . log ( itCompanies . splice ( 2 , 1 ) ) ;
87
+
88
+ // Remove the last IT company from the array
89
+ console . log ( itCompanies . pop ( ) ) ;
90
+
91
+ // Remove all IT companies
92
+ console . log ( itCompanies . splice ( ) ) ;
93
+
94
+ // First remove all the punctuations and change the string to array and count the number of words in the array
95
+ let text = 'I love teaching and empowering people. I teach HTML, CSS, JS, React, Python.'
96
+ console . log ( text ) ;
97
+ let newText = text . split ( ' ' ) ;
98
+ console . log ( newText ) ;
99
+ console . log ( newText . length ) ;
100
+
101
+ // In the following shopping cart add, remove, edit items
102
+ const shoppingCart = [ 'Milk' , 'Coffee' , 'Tea' , 'Honey' ] ;
103
+ console . log ( shoppingCart ) ;
104
+
105
+ // add 'Meat' in the beginning of your shopping cart if it has not been already added
106
+ shoppingCart . unshift ( 'Meat' ) ;
107
+ console . log ( shoppingCart ) ;
108
+
109
+ // add Sugar at the end of you shopping cart if it has not been already added
110
+ shoppingCart . push ( 'Sugar' ) ;
111
+ console . log ( shoppingCart ) ;
112
+
113
+ // remove 'Honey' if you are allergic to honey
114
+ shoppingCart . splice ( 4 , 1 ) ;
115
+ console . log ( shoppingCart ) ;
116
+
117
+ // modify Tea to 'Green Tea'
118
+ shoppingCart [ 3 ] = 'Green Tea' ;
119
+ console . log ( shoppingCart ) ;
120
+
121
+ // Concatenate the following two variables and store it in a fullStack variable.
122
+ const frontEnd = [ 'HTML' , 'CSS' , 'JS' , 'React' , 'Redux' ]
123
+ const backEnd = [ 'Node' , 'Express' , 'MongoDB' ]
124
+ const fullStack = frontEnd . concat ( backEnd ) ;
125
+
126
+ console . log ( fullStack ) ;
127
+
128
+ // Sort the array and find the min and max age
129
+ const ages = [ 19 , 22 , 19 , 24 , 20 , 25 , 26 , 24 , 25 , 24 ]
130
+ console . log ( ages . sort ( ) ) ;
131
+
132
+ // minimum age
133
+ let minAge = Math . min . apply ( null , ages ) ;
134
+ console . log ( minAge ) ;
135
+
136
+ // minimum age
137
+ let maxAge = Math . max . apply ( null , ages ) ;
138
+ console . log ( maxAge ) ;
139
+
140
+ // Find the median age(one middle item or two middle items divided by two)
141
+ let medianAge = ( ( ages [ 4 ] + ages [ 5 ] ) / 2 ) ;
142
+ console . log ( medianAge ) ;
143
+
144
+ // Find the average age(all items divided by number of items)
145
+ let averageAge = ( ages / ages . length ) ;
146
+ console . log ( averageAge ) ;
0 commit comments