-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
60 lines (50 loc) · 2.11 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
const dates = document.querySelectorAll('.weather-item h3');
const weatherImgs = document.querySelectorAll('.weather-img');
const weatherCndtns = document.querySelectorAll('.weather-condition');
const minTemps = document.querySelectorAll('.temp .min');
const maxTemps = document.querySelectorAll('.temp .max');
const city = document.querySelector('.weather-display h2');
// The Search Form
const cityForm = document.getElementById('cityForm');
const cityInput = document.getElementById('city');
const submitBtn = document.getElementById('submit');
// URL and Options for the API
const url = 'https://weatherapi-com.p.rapidapi.com/forecast.json?q=Lagos&days=3';
const options = {
method: 'GET',
headers: {
'X-RapidAPI-Key': 'ef04bb25f7msh213900c577585e1p1f01e0jsna21b2c903032',
'X-RapidAPI-Host': 'weatherapi-com.p.rapidapi.com'
}
};
async function weather(url, options) {
try {
const response = await fetch(url, options);
const result = await response.json();
console.log(result);
const forecast = result.forecast;
for (let i = 0; i < forecast.forecastday.length; i++) {
weatherImgs[i].src = `${forecast.forecastday[i].day.condition.icon}`;
weatherImgs[i].alt = forecast.forecastday[i].day.condition.text;
weatherCndtns[i].innerHTML = forecast.forecastday[i].day.condition.text;
minTemps[i].innerHTML = forecast.forecastday[i].day.mintemp_c + '℃';
maxTemps[i].innerHTML = forecast.forecastday[i].day.maxtemp_c + '℃';
var date = new Date(forecast.forecastday[i].date);
dates[i].innerHTML = date.toDateString();
}
city.textContent = `${result.location.name}, ${result.location.country}`;
} catch (error) {
console.error(error);
}
}
weather(url, options);
cityForm.addEventListener('submit', (e) => {
e.preventDefault();
var url = `https://weatherapi-com.p.rapidapi.com/forecast.json?q=${cityInput.value}&days=3`;
try {
city.textContent = cityInput.value;
weather(url, options);
} catch (error) {
console.error(error);
}
})