-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
36 lines (25 loc) · 786 Bytes
/
Copy pathscript.js
File metadata and controls
36 lines (25 loc) · 786 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
30
31
32
33
34
35
36
async function getWeather() {
let city = document.getElementById("city").value;
let apiKey = "b41fdc94bb733178c11ba95eeee07d3af";
let url = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}&units=metric`;
try {
let response = await fetch(url);
let data = await response.json();
if (data.cod != 200) {
document.getElementById("result").innerHTML = "City not found or API issue";
return;
}
let temp = data.main.temp;
let humidity = data.main.humidity;
let weather = data.weather[0].description;
document.getElementById("result").innerHTML = `
<h3>${city}</h3>
<p>Temperature: ${temp} °C</p>
<p>Humidity: ${humidity}%</p>
<p>Condition: ${weather}</p>
`;
}
catch(error) {
document.getElementById("result").innerHTML = "Error fetching weather data";
}
}