-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
39 lines (34 loc) · 1.28 KB
/
script.js
File metadata and controls
39 lines (34 loc) · 1.28 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
34
35
36
37
38
39
const BASE_URL = "https://dataservice.accuweather.com";
const API_KEY = "KGbpl82jSGcs6sfPI0OXuZF5sAaNhBYB";
const search = document.getElementById("search");
search.addEventListener("submit", getWeatherForecast);
function getWeatherForecast(event) {
event.preventDefault();
const city = document.getElementById("city").value.trim();
getLocationKey(city);
}
function getLocationKey(city) {
fetch(`${BASE_URL}/locations/v1/cities/search?apikey=${API_KEY}&q=${city}`)
.then((response) => response.json())
.then((data) => {
const location = data[0];
getCurrentCondition(location);
})
.catch((err) => console.log(err));
}
function getCurrentCondition(location) {
fetch(`${BASE_URL}/currentconditions/v1/${location.Key}?apikey=${API_KEY}`)
.then((response) => response.json())
.then((data) => {
const forecast = data[0];
updateUI(location, forecast);
})
.catch((err) => console.log(err));
}
function updateUI(location, forecast) {
document.getElementById("name").innerText = location.LocalizedName;
document.getElementById(
"condition"
).innerHTML = `<i class="wi icon-accu${forecast.WeatherIcon}"></i> ${forecast.WeatherText}`;
document.getElementById("temperature").innerHTML = `${forecast.Temperature.Imperial.Value} ℉`;
}