I Do have show and getweather function but fails the test
Also i get error for
5. The getWeather function should accept a city as its only argument and return the JSON from the Weather API.
16. The showWeather function should call the getWeather function to get the weather data.
17. The showWeather function should manage the case in which getWeather returns undefined.
23. If there is an error, your getWeather function should log the error to the console.
which is driving me nuts
<!-- file: index.html -->
/* file: styles.css */
/* file: script.js */
async function showWeather(city) {
const data = await getWeather(city);
if (!data) { // handle undefined
console.log("No data received from getWeather");
return;
}
if(data.error) { // handle API error
console.log("API returned an error:", data.error);
alert("Something went wrong, please try again later.")
return;
}
locationSpan.innerHTML = data.name;
weatherMainSpan.innerHTML = data.weather[0].main;
weatherIconSpan.src = data.weather[0].icon;
mainTemp.innerHTML = `Temp: ${data.main.temp}°C`
feelsLike.innerHTML = `Feels Like: ${data.main.feels_like}°C`
humidity.innerHTML = `Humidity: ${data.main.humidity}%`
pressure.innerHTML = `Pressure: ${data.main.pressure} hPa`
windSpeed.innerHTML = `Wind: ${data.wind.speed + " m/s"}`
windGusts.innerHTML = `Gusts: ${data.wind.gust === undefined ? "N/A": (data.wind.gust + " m/s")}`
}
async function getWeather(city){
try {
const res = await fetch(weatherUrl + city);
const data = await res.json();
if(city === 'paris') alert("Something went wrong, please try again later.")
return data; // Always return JSON
} catch (err) {
console.error(err)
return undefined; // Explicitly return undefined if fetch fails
}
}
getWeatherBtn.addEventListener("click", () => {
if (cityOptions.value === "") return;
showWeather(cityOptions.value)
})
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:149.0) Gecko/20100101 Firefox/149.0
Challenge Information:
Build a Weather App - Build a Weather App
