fix if else statement in checking items in a list

pull/118/head
Kamrul Islam Shahin 5 years ago
parent 7bf81befc0
commit db042caeda

@ -390,22 +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 does not exist in the array')
console.log('This fruit exists in the array')
}
// This fruit does 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 does not exist in the array')
index === -1
? console.log('This fruit does exist in the array')
: console.log('This fruit exists 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 does not exist in the array')
console.log('This fruit exists in the array')
}
// This fruit does not exist in the array
```

Loading…
Cancel
Save