-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
34 lines (29 loc) · 1.21 KB
/
script.js
File metadata and controls
34 lines (29 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const apiKey = 'c2d34e085ba6d0afd50480dd1329cd4d';
async function getWeather() {
const city = document.getElementById('city').value;
if (!city) {
alert('Please enter a city name.');
return;
}
const url = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}&units=metric`;
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error('City not found');
}
const weatherData = await response.json();
displayWeather(weatherData);
} catch (error) {
alert(error.message);
}
}
function displayWeather(data) {
const weatherResult = document.getElementById('weather-result');
weatherResult.innerHTML = `
<div class="weather-info"><strong>City:</strong> ${data.name}</div>
<div class="weather-info"><strong>Temperature:</strong> ${data.main.temp} °C</div>
<div class="weather-info"><strong>Weather:</strong> ${data.weather[0].description}</div>
<div class="weather-info"><strong>Humidity:</strong> ${data.main.humidity} %</div>
<div class="weather-info"><strong>Wind Speed:</strong> ${data.wind.speed} m/s</div>
`;
}