@@ -70,12 +70,12 @@ const countries = ['Finland', 'Estonia', 'Sweden', 'Norway']
70
70
countries .forEach ((country , i ) => console .log (i, country .toUpperCase ()))
71
71
```
72
72
73
- output:
74
-
73
+ ``` sh
75
74
0 " FINLAND"
76
75
1 " ESTONIA"
77
76
2 " SWEDEN"
78
77
3 " NORWAY"
78
+ ```
79
79
80
80
For example if we like to change each country to uppercase and store it back to an array we write it as follows.
81
81
@@ -85,8 +85,11 @@ const newCountries = []
85
85
countries .forEach (country => newCountries .push (country))
86
86
87
87
console .log (newCountries) // ["Finland", "Estonia", "Sweden", "Norway"]
88
+ ```
89
+
88
90
Let us see more examples. For instance if we want to sum an array of numbers we can use forEach or reduce. Let us see how we use forEach to sum all numbers in an array.
89
91
92
+ ``` js
90
93
const numbers = [1 , 2 , 3 , 4 , 5 ]
91
94
let sum = 0
92
95
numbers .forEach ((n ) => sum += n)
@@ -165,7 +168,6 @@ For instance if we want to filter out countries containing a substring land from
165
168
166
169
``` js
167
170
// syntax in a normal or a function declaration
168
-
169
171
function callback (item ) {
170
172
return // boolean
171
173
}
@@ -178,10 +180,11 @@ const callback = (item) => {
178
180
return // boolean
179
181
}
180
182
const filteredArray = array .filter (callback)
183
+ ```
181
184
185
+ ``` js
182
186
const countries = [' Finland' , ' Estonia' , ' Sweden' , ' Norway' , ' Iceland' ]
183
187
const countriesWithLand = countries .filter (country => country .includes (' land' ))
184
-
185
188
console .log (countriesWithLand) // ["Finland", "Iceland"]
186
189
```
187
190
@@ -190,7 +193,6 @@ How about if we want to filter out countries not containing the substring land.
190
193
``` js
191
194
const countries = [' Finland' , ' Estonia' , ' Sweden' , ' Norway' , ' Iceland' ]
192
195
const countriesWithLand = countries .filter (country => ! country .includes (' land' ))
193
-
194
196
console .log (countriesWithLand) // ["Estonia", "Sweden", "Norway"]
195
197
```
196
198
@@ -200,9 +202,7 @@ Let's see an additional example about the filter, let us filter even or odd numb
200
202
const numbers = [0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ]
201
203
const evens = numbers .filter (n => n % 2 === 0 )
202
204
const odds = numbers .filter (n => n % 2 !== 0 )
203
-
204
205
console .log (evens) // [0, 2, 4, 6, 8, 10]
205
-
206
206
console .log (odds) // [1, 3, 5, 7, 9]
207
207
```
208
208
@@ -300,7 +300,6 @@ Let find the first even number and the first odd number in the numbers array.
300
300
const numbers = [0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ]
301
301
const firstEvenNum = numbers .find ((n ) => n % 2 === 0 )
302
302
const firstOddNum = numbers .find ((n ) => n % 2 !== 0 )
303
-
304
303
console .log (firstEvenNum) // 0
305
304
console .log (firstOddNum) // 1
306
305
```
@@ -357,7 +356,6 @@ const numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
357
356
const firstEvenIndex = numbers .findIndex ((n ) => n % 2 === 0 )
358
357
const firstOddIndex = numbers .findIndex ((n ) => n % 2 !== 0 )
359
358
console .log (firstEvenIndex) // 0
360
-
361
359
console .log (firstOddIndex) // 1
362
360
` ` `
363
361
@@ -389,7 +387,6 @@ In the following array some numbers are even and some are odd, so if I ask you a
389
387
const numbers = [0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ]
390
388
const someAreEvens = numbers .some ((n ) => n % 2 === 0 )
391
389
const someAreOdds = numbers .some ((n ) => n % 2 !== 0 )
392
-
393
390
console .log (someAreEvens) // true
394
391
console .log (someAreOdds) // true
395
392
` ` `
@@ -400,7 +397,6 @@ Let us another example
400
397
const evens = [0 , 2 , 4 , 6 , 8 , 10 ]
401
398
const someAreEvens = evens .some ((n ) => n % 2 === 0 )
402
399
const someAreOdds = evens .some ((n ) => n % 2 !== 0 )
403
-
404
400
console .log (someAreEvens) // true
405
401
console .log (someAreOdds) // false
406
402
` ` `
0 commit comments