-
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) · 979 Bytes
/
script.js
File metadata and controls
36 lines (25 loc) · 979 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
const apiUrl = "http://api.weatherapi.com/v1/forecast.json?key=2e65ec2382ca464dac1191857242801&q=Nairobi&days=1&aqi=no&alerts=no";
const container = document.getElementById('weatherContainer');
fetch(apiUrl)
.then(res => res.json()) // convert response to JSON
.then(data => {
// Getting the forecast object
const forecastObject = data.forecast;
const forecastDaysArray = forecastObject.forecastday;
const todayForecast = forecastDaysArray[0];
const hourlyForecast = todayForecast.hour;
const hours = hourlyForecast;
hours.forEach(hour => {
const card = document.createElement("div");
card.className = "weather-card";
// Filling the card
card.innerHTML = `
<strong>${hour.time.slice(11)}</strong><br>
Temp: ${hour.temp_c} °C<br>
Cloud: ${hour.cloud} %<br>
Wind: ${hour.wind_kph} kph<br>
Dir: ${hour.wind_dir}
`;
container.appendChild(card);
});
});