-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
65 lines (60 loc) · 1.64 KB
/
index.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
// API Docs: https://www.weather.gov/documentation/services-web-api
var date;
var apiUrl;
// Fetch weather data and output it to the page
const fetchAPI = function() {
// Define User-Agent, required by the U.S. Weather Service API
let headers = new Headers({
"User-Agent" : "github.com/k-stamps"
});
fetch(apiUrl, {
method : 'GET',
headers : headers
})
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
// Display data in an HTML element
var PeriodName = data["properties"]["periods"][0]["name"]
var PeriodForecast = data["properties"]["periods"][0]["detailedForecast"]
document.getElementById("city").textContent = cityName;
document.getElementById("periodName").textContent = PeriodName.toLowerCase();
document.getElementById("periodForecast").textContent = PeriodForecast.toLowerCase();
date = new Date();
document.getElementById("dateNow").textContent = date;
})
.catch(error => {
console.error('Error:', error);
});
};
// Define city
var cityName;
var e = document.getElementById("city-select");
function onChange() {
cityName = e.value;
changeCity(cityName);
fetchAPI();
}
e.onchange = onChange;
onChange();
//GRIDPOINTS forecastGridData URL:
function changeCity(cityName) {
var city;
switch(cityName) {
case "New York":
city = 'OKX/33,36';
break;
case "Chicago":
city = 'LOT/75,72';
break;
case "Los Angeles":
city = 'LOX/148,41';
break;
}
// Define the API URL
apiUrl = 'https://api.weather.gov/gridpoints/'+city+'/forecast';
}