Beginning coder here taking a js course. I'm almost done with a higher order function lesson, but am stuck. I have an object containing 250 countries with different info. Example:
const countries = [
{
name: 'Afghanistan',
capital: 'Kabul',
languages: ['Pashto', 'Uzbek', 'Turkmen'],
population: 27657145,
flag:
'https://restcountries.eu/data/afg.svg',
currency: 'Afghan afghani'
},
{
name: 'Åland Islands',
capital: 'Mariehamn',
languages: ['Swedish'],
population: 28875,
flag:
'https://restcountries.eu/data/ala.svg',
currency: 'Euro'
}, etc.
I'm asked to write a function that searches each country's name and returns an array of only the countries meeting the keyword criteria. I'm stumped and feel very lost. Here's what I have:
const keys = Object.keys(countries)
function categorizeCountries(keyword) {
for (let i = 0; i < keys.length; i++) {
let country = countries.name
if (country.includes(keyword)) {
console.log(countries.filter((country)
=> country.includes(keyword)))
} else {
console.log('Country not found')
}
}
return country
}
categorizeCountries('land')
scategorizeCountries('stan')
I'm sure the issue is in my conditional statement, but I don't know how else to go about this. Any help is greatly appreciated.