|
|
|
@ -390,24 +390,24 @@ Check an element if it exist in an array.
|
|
|
|
|
const fruits = ['banana', 'orange', 'mango', 'lemon']
|
|
|
|
|
let index = fruits.indexOf('banana') // 0
|
|
|
|
|
|
|
|
|
|
if(index === -1) {
|
|
|
|
|
if(index != -1) {
|
|
|
|
|
console.log('This fruit does exist in the array')
|
|
|
|
|
} else {
|
|
|
|
|
console.log('This fruit exists in the array')
|
|
|
|
|
console.log('This fruit does not exist in the array')
|
|
|
|
|
}
|
|
|
|
|
// This fruit exists in the array
|
|
|
|
|
|
|
|
|
|
// we can use also ternary here
|
|
|
|
|
index === -1
|
|
|
|
|
? console.log('This fruit does exist in the array')
|
|
|
|
|
: console.log('This fruit exists in the array')
|
|
|
|
|
index != -1
|
|
|
|
|
? console.log('This fruit does exist in the array')
|
|
|
|
|
: console.log('This fruit does not exist in the array')
|
|
|
|
|
|
|
|
|
|
// let us check if a avocado exist in the array
|
|
|
|
|
let indexOfAvocado = fruits.indexOf('avocado') // -1, if the element not found index is -1
|
|
|
|
|
if(indexOfAvocado === -1) {
|
|
|
|
|
if(indexOfAvocado != -1) {
|
|
|
|
|
console.log('This fruit does exist in the array')
|
|
|
|
|
} else {
|
|
|
|
|
console.log('This fruit exists in the array')
|
|
|
|
|
console.log('This fruit does not exist in the array')
|
|
|
|
|
}
|
|
|
|
|
// This fruit does not exist in the array
|
|
|
|
|
```
|
|
|
|
|