-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
21 lines (20 loc) · 765 Bytes
/
script.js
File metadata and controls
21 lines (20 loc) · 765 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
async function getWeather() {
const city = document.getElementById("city").value;
const resultDiv = document.getElementById("weatherResult");
if (city === "") {
resultDiv.innerHTML = "Enter a city name";
return;
}
try {
const response = await fetch(`https://wttr.in/${city}?format=j1`);
const data = await response.json();
resultDiv.innerHTML = `
<h3>${city}</h3>
<p>Temperature: ${data.current_condition[0].temp_C} °C</p>
<p>Humidity: ${data.current_condition[0].humidity}%</p>
<p>Weather: ${data.current_condition[0].weatherDesc[0].value}</p>
`;
} catch (error) {
resultDiv.innerHTML = "City not found";
}
}