@ -3635,15 +3632,15 @@ If we are interested in the first occurrence of a certain item or element in an
// syntax in a normal or a function declaration
// syntax in a normal or a function declaration
function callback(item) {
function callback(item) {
return // code goes here
return // code goes here
}
}
const item = array.find(callback)
const item = array.find(callback)
// or syntax in an arrow function
// or syntax in an arrow function
const reduced = callback(item) => {
const reduced = callback(item) => {
return // code goes here
return // code goes here
}
}
const item = array.find(callback)
const item = array.find(callback)
```
```
@ -3679,7 +3676,7 @@ Let us find the first country in the array which has the letter 'o'
```js
```js
const countries = ['Finland', 'Estonia', 'Sweden', 'Norway', 'Iceland']
const countries = ['Finland', 'Estonia', 'Sweden', 'Norway', 'Iceland']
const index = countries.find((country) => country.includes('o'))
const index = countries.find((country) => country.includes('o'))
console.log(index // Estonia
console.log(index) // Estonia
```
```
#### 6. findIndex
#### 6. findIndex
@ -3690,15 +3687,15 @@ The findIndex method works like find but findIndex returns the index of the item
// syntax in a normal or a function declaration
// syntax in a normal or a function declaration
function callback(item) {
function callback(item) {
return // code goes here
return // code goes here
}
}
const index = array.findIndex(callback)
const index = array.findIndex(callback)
// or syntax in an arrow function
// or syntax in an arrow function
const reduced = callback(item) => {
const reduced = callback(item) => {
return // code goes here
return // code goes here
}
}
const index = array.findIndex(callback)
const index = array.findIndex(callback)
```
```
@ -3718,7 +3715,7 @@ Let us find the index of the first country in the array which has exactly six ch
```js
```js
const countries = ['Finland', 'Estonia', 'Sweden', 'Norway', 'Iceland']
const countries = ['Finland', 'Estonia', 'Sweden', 'Norway', 'Iceland']
const index = countries.findIndex((country) => country.length === 6)
const index = countries.findIndex((country) => country.length === 6)
console.log(index //2
console.log(index) //2
```
```
Let us find the index of the first country in the array which has the letter 'o'.
Let us find the index of the first country in the array which has the letter 'o'.
@ -3726,7 +3723,7 @@ Let us find the index of the first country in the array which has the letter 'o'
```js
```js
const countries = ['Finland', 'Estonia', 'Sweden', 'Norway', 'Iceland']
const countries = ['Finland', 'Estonia', 'Sweden', 'Norway', 'Iceland']
const index = countries.findIndex((country) => country.includes('o'))
const index = countries.findIndex((country) => country.includes('o'))
console.log(index // 1
console.log(index) // 1
```
```
Let us move on to the next functional programming, some.
Let us move on to the next functional programming, some.
@ -3872,7 +3869,7 @@ Let use the class constructor to pass different properties for the class.
The constructor is a builtin function which allows as to create a blueprint for our object. The constructor function starts with a keyword constructor followed by a parenthesis. Inside the parenthesis we pass the properties of the object as parameter. We use the _this_ keyword to attach the constructor parameters with the class.
The constructor is a builtin function which allows as to create a blueprint for our object. The constructor function starts with a keyword constructor followed by a parenthesis. Inside the parenthesis we pass the properties of the object as parameter. We use the _this_ keyword to attach the constructor parameters with the class.
The following Person class constructor has firstName and lastName property. These properties are attached to the Person class using _this_ keyword. _This_ refers to the class itself.
The following Person class constructor has firstName and lastName property. These properties are attached to the Person class using _this_ keyword. _this_ refers to the class itself.