Hi @KarimZ, that probably means that you didn’t find() anything that matches the country name, in which case undefined is returned. So you’d have to verify that there is a match first:
if (result) {
focusedName.innerHTML = result.name
}
focusedName.innerHTML = result ? result.name : 'No match found'
BTW why are you using const for the result, but let for the element? Personally I’d prefer const over let whenever possible, but there are good reasons for either convention. At any rate, I’d keep it consistent.
The first function returns a country name as it reads what’s inside a H1 element, I don’t get undefined in return. I want to use the string inside the h1inside the infoTemplate function.
Because if I change the countryName inside the second function to a string of a country, the function works fine.
Ah yes, there’s another problem. You’re assigning the return value of addEventListener() to countryName, which is also undefined – what you are returning from inside the listener callback doesn’t go anywhere. That function will get called back at some point in the future (namely when the user clicks .searchResult), but infoTemplate() is getting called immediately:
Add event listener
Render info template
User clicks element – where should the return value go?
So you’d have to call infoTemplate() inside the event listener callback, and pass the country name:
function infoTemplate (countryName) {
getData().then((data) => {
const result = data.find(({ name }) => name === countryName)
const focusedName = document.querySelector('.focusedName')
focusedName.innerHTML = result ? result.name : 'No match found'
})
}
document
.querySelector('.searchResult')
.addEventListener('click', (e) => {
const target = e.target.lastChild.innerHTML
infoTemplate(target)
})