-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
33 lines (27 loc) · 1.04 KB
/
script.js
File metadata and controls
33 lines (27 loc) · 1.04 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
async function getWeather() {
const location = document.getElementById("locationInput").value.trim();
const resultArea = document.getElementById("resultArea");
if (!location) {
resultArea.textContent = "Please enter a location.";
return;
}
const apiKey = "eab14be40f5d41aeae3185037251007";
const url = `http://api.weatherapi.com/v1/current.json?key=${apiKey}&q=${encodeURIComponent(location)}&aqi=yes`;
try {
resultArea.textContent = "Loading...";
const response = await fetch(url);
if (!response.ok) {
throw new Error("Location not found.");
}
const data = await response.json();
const tempC = data.current.temp_c;
const condition = data.current.condition.text;
resultArea.innerHTML = `
<strong>${data.location.name}, ${data.location.country}</strong><br>
Temperature: ${tempC}°C<br>
Condition: ${condition}
`;
} catch (error) {
resultArea.textContent = error.message;
}
}