-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
29 lines (26 loc) · 905 Bytes
/
script.js
File metadata and controls
29 lines (26 loc) · 905 Bytes
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
async function getWeather() {
const apiKey = "API_KEY";
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 data = await response.json();
document.getElementById("weather").innerHTML = `
<h3>${data.name}, ${data.sys.country}</h3>
<p>Temperature: ${data.main.temp}°C</p>
<p>Humidity: ${data.main.humidity}%</p>
<p>Weather: ${data.weather[0].description}</p>
`;
} catch (error) {
document.getElementById(
"weather"
).innerHTML = `<p style="color:red;">${error.message}</p>`;
}
}