Reputation: 43
I am trying to solve the following problem: If there are countries containing 'land', print it as array. If there is no country containing the word 'land', print 'All these are countries without land'. The problem is that when I put if-else statements within the loop it prints all steps in iteration which I do not need. However, outside the loop, I can print only one statement. So where I should put the message when the condition fails? Please help, it seems simple but I couldn't solve it.
for(let i = 0; i < countries.length; i++){
if(countries[i].includes('land'))
arr.push(countries[i])
}
console.log(arr)
Upvotes: 0
Views: 212
Reputation: 6914
you know that if there was atleast one country containing word land
then arr
will not have length of zero
you can use this length to verify if there was no country found
for(let i = 0; i < countries.length; i++){
if(countries[i].includes('land'))
arr.push(countries[i])
}
if(arr.length > 0 )
console.log(arr);
else
console.log('All these are countries without land')
Upvotes: 1