Skip to content

Commit 385d348

Browse files
committed
initial commit
1 parent f6a7f47 commit 385d348

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

readme.md

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ const countries = ['Finland', 'Estonia', 'Sweden', 'Norway']
7070
countries.forEach((country, i) => console.log(i, country.toUpperCase()))
7171
```
7272

73-
output:
74-
73+
```sh
7574
0 "FINLAND"
7675
1 "ESTONIA"
7776
2 "SWEDEN"
7877
3 "NORWAY"
78+
```
7979

8080
For example if we like to change each country to uppercase and store it back to an array we write it as follows.
8181

@@ -85,8 +85,11 @@ const newCountries = []
8585
countries.forEach(country => newCountries.push(country))
8686

8787
console.log(newCountries) // ["Finland", "Estonia", "Sweden", "Norway"]
88+
```
89+
8890
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.
8991

92+
```js
9093
const numbers = [1, 2, 3, 4, 5]
9194
let sum = 0
9295
numbers.forEach((n) => sum += n)
@@ -165,7 +168,6 @@ For instance if we want to filter out countries containing a substring land from
165168

166169
```js
167170
// syntax in a normal or a function declaration
168-
169171
function callback(item) {
170172
return // boolean
171173
}
@@ -178,10 +180,11 @@ const callback = (item) => {
178180
return // boolean
179181
}
180182
const filteredArray = array.filter(callback)
183+
```
181184

185+
```js
182186
const countries = ['Finland', 'Estonia', 'Sweden', 'Norway', 'Iceland']
183187
const countriesWithLand = countries.filter(country => country.includes('land'))
184-
185188
console.log(countriesWithLand) // ["Finland", "Iceland"]
186189
```
187190

@@ -190,7 +193,6 @@ How about if we want to filter out countries not containing the substring land.
190193
```js
191194
const countries = ['Finland', 'Estonia', 'Sweden', 'Norway', 'Iceland']
192195
const countriesWithLand = countries.filter(country => !country.includes('land'))
193-
194196
console.log(countriesWithLand) // ["Estonia", "Sweden", "Norway"]
195197
```
196198

@@ -200,9 +202,7 @@ Let's see an additional example about the filter, let us filter even or odd numb
200202
const numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
201203
const evens = numbers.filter(n => n % 2 === 0)
202204
const odds = numbers.filter(n => n % 2 !== 0)
203-
204205
console.log(evens) // [0, 2, 4, 6, 8, 10]
205-
206206
console.log(odds) // [1, 3, 5, 7, 9]
207207
```
208208

@@ -300,7 +300,6 @@ Let find the first even number and the first odd number in the numbers array.
300300
const numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
301301
const firstEvenNum = numbers.find((n) => n % 2 === 0)
302302
const firstOddNum = numbers.find((n) => n % 2 !== 0)
303-
304303
console.log(firstEvenNum) // 0
305304
console.log(firstOddNum) // 1
306305
```
@@ -357,7 +356,6 @@ const numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
357356
const firstEvenIndex = numbers.findIndex((n) => n % 2 === 0)
358357
const firstOddIndex = numbers.findIndex((n) => n % 2 !== 0)
359358
console.log(firstEvenIndex) // 0
360-
361359
console.log(firstOddIndex) // 1
362360
```
363361
@@ -389,7 +387,6 @@ In the following array some numbers are even and some are odd, so if I ask you a
389387
const numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
390388
const someAreEvens = numbers.some((n) => n % 2 === 0)
391389
const someAreOdds = numbers.some((n) => n % 2 !== 0)
392-
393390
console.log(someAreEvens) // true
394391
console.log(someAreOdds) // true
395392
```
@@ -400,7 +397,6 @@ Let us another example
400397
const evens = [0, 2, 4, 6, 8, 10]
401398
const someAreEvens = evens.some((n) => n % 2 === 0)
402399
const someAreOdds = evens.some((n) => n % 2 !== 0)
403-
404400
console.log(someAreEvens) // true
405401
console.log(someAreOdds) // false
406402
```

0 commit comments

Comments
 (0)