-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
92 lines (48 loc) · 1.9 KB
/
app.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
load();
const API_URL = "http://api.openweathermap.org/geo/1.0/direct";
const API_KEY = "3bd30b42dbce1f32d597d94dd8482394";
async function getCityData(cityName) {
try {
const url = `${API_URL}?q=${cityName}&limit=5&appid=${API_KEY}`;
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const data = await response.json();
console.log("City Data:", data);
return data;
} catch (error) {
console.error("Error fetching city data:", error);
}
}
getCityData(document.getElementById(searchBar));
function load() {
const locations = [
{ id: "newYork", name: "New York" },
{ id: "london", name: "London" },
{ id: "tokyo", name: "Tokyo" },
{ id: "paris", name: "Paris" },
{ id: "beijing", name: "Beijing" },
{ id: "dubai", name: "Dubai" },
{ id: "sydney", name: "Sydney" },
{ id: "mumbai", name: "Mumbai" },
{ id: "rome", name: "Rome" },
{ id: "saoPaulo", name: "Sao Paulo" }
];
locations.forEach(location => {
const url = `https://api.weatherapi.com/v1/current.json?key=${apiKey}&q=${location.name}`;
fetch(url)
.then(res => res.json())
.then(data => {
document.getElementById(`${location.id}TempC`).innerHTML =
"Temp: " + data.current.temp_c + "°C";
document.getElementById(`${location.id}Wind`).innerHTML =
"Wind: " + data.current.wind_kph + " kmph";
})
.catch(err => {
console.error("Failed to fetch weather data for " + location.name, err);
document.getElementById(`${location.id}TempC`).innerHTML = "Error loading data";
document.getElementById(`${location.id}Wind`).innerHTML = "Hello";
});
});
}