Tell us what’s happening:
test number 18 says ( * 18. When New York is selected the showWeather function should display the data from the API in the respective HTML elements. If the value from the API is undefined, you should show N/A.
i succeed handled the data and returned it as it must be - i think - but iam not passing the test yet, anyhelp please ?
showWeather
function should display the data from the API in the respective HTML elements. If the value from the API is
undefined
, you should show
N/A
.)
Your code so far
<!-- file: index.html -->
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Weather App</title>
</head>
<body>
<div id='container'>
<img id='weather-icon' src='' alt='' />
<main id='main-temperature'>
</main>
<section id='feels-like'>Feels Like:
</section>
<section id='humidity'>
Humidty:
</section>
<section id='wind'>
Wind:
</section>
<section id='wind-gust'>
Gusts:
</section>
<section id='weather-main'>
</section>
<section id='location'>
</section>
</div>
<select id='drop-down'>
<option value=''></option>
<option value='paris'>Paris</option>
<option value='london'>London</option>
<option value='tokyo'>Tokyo</option>
<option value='los angeles'>Los Angeles</option>
<option value='chicago'>Chicago</option>
<option value='new york'>New York</option>
</select>
<button id='get-weather-btn'>Get Weather</button>
</body>
<script src='./script.js'>
</script>
</html>
/* file: script.js */
const weatherBtn = document.getElementById('get-weather-btn');
const dropDownMenu = document.getElementById('drop-down');
const weatherIcon = document.getElementById('weather-icon');
const mainTemperature = document.getElementById('main-temperature');
const feelsLike = document.getElementById('feels-like');
const humidity = document.getElementById('humidity');
const wind = document.getElementById('wind');
const windGust = document.getElementById('wind-gust');
const weatherMain = document.getElementById('weather-main');
const locationI = document.getElementById('location');
dropDownMenu.addEventListener('input', (e) => {
if(!e.target.value) {
weatherBtn.disabled = true
weatherBtn.style.cursor = 'not-allowed'
}
weatherBtn.style.cursor = 'pointer'
weatherBtn.disabled = false
})
const showWeather = async (city) => {
getWeather(dropDownMenu.value)
}
async function getWeather (city) {
try {
const res = await fetch(`https://weather-proxy.freecodecamp.rocks/api/city/${city}` )
const data = await res.json()
console.log(data)
// console.log(data.weather[0])
weatherIcon.src = data.weather[0].icon
data.main.temp ?
mainTemperature.textContent = data.main.temp + '°' + 'C'
: mainTemperature.textContent = 'N/A'
feelsLike.textContent = 'Feels Like: ' + data.main['feels_like'] || 'N/A'
data.main.humidity ?
humidity.textContent = 'Humidty: ' + data.main.humidity
: humidity.textContent = 'N/A'
data.wind.speed ?
wind.textContent = 'Wind: ' + data.wind.speed
: wind.textContent = 'N/A'
data.wind.gust ?
windGust.textContent = 'Gusts: ' + data.wind.gust
: windGust.textContent = 'N/A'
weatherMain.textContent = data.weather.main
return data
}catch(error) {
alert('Something went wrong, please try again later')
}
}
weatherBtn.addEventListener('click', () => {
if(!dropDownMenu.value) {
return
}
if(dropDownMenu.value == 'paris') {
alert('Something went wrong, please try again later')
}
if(!showWeather()) {
mainTemperature.textContent = 'N/A'
}
})
/* file: styles.css */
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36
Challenge Information:
Build a Weather App - Build a Weather App
