-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
32 lines (27 loc) · 1 KB
/
script.js
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
// script.js
const apiKey = '516b57cbc9397264cdc68e1538604455';
async function getWeather() {
const location = document.getElementById('locationInput').value;
const apiUrl = `https://api.openweathermap.org/data/2.5/weather?q=${location}&units=metric&appid=${apiKey}`;
try {
const response = await fetch(apiUrl);
const data = await response.json();
if (response.ok) {
displayWeather(data);
} else {
// Handle city not found error
alert('City not found. Please check the spelling.');
}
} catch (error) {
console.error('Error fetching weather data:', error);
}
}
function displayWeather(data) {
const weatherInfo = document.getElementById('weatherInfo');
weatherInfo.innerHTML = `
<p>City: ${data.name}</p>
<p>Temperature: ${data.main.temp}°C</p>
<p>Description: ${data.weather[0].description}</p>
<p>Wind Speed: ${data.wind.speed} m/s</p>
`;
}