-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
67 lines (67 loc) · 3.39 KB
/
script.js
File metadata and controls
67 lines (67 loc) · 3.39 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
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
document.getElementById("weatherSubmit").addEventListener("click", event => {
event.preventDefault();
const value = document.getElementById("weatherInput").value;
if (value === "")
return;
console.log(value);
// GET CURRENT WEATHER IN GIVEN CITY (NAME)
const url = "http://api.openweathermap.org/data/2.5/weather?q=" + value + ",US&units=imperial" + "&APPID=6cb68e866fb8a28497eb7120da1d1ba2";
fetch(url)
.then(response => {
return response.json();
}).then(json => {
console.log(json);
// test
let sunrise = new Date((json.sys.sunrise) * 1000);
let sunset = new Date((json.sys.sunset) * 1000);
console.log(moment(sunrise).format('h:mm a'));
// end test
let results = "";
results += '<div class="signup-header2"><h1 class="header2">Weather in ' + json.name + "</h1>";
for (let i=0; i < json.weather.length; i++) {
results += '<img src="http://openweathermap.org/img/w/' + json.weather[i].icon + '.png"/>';
}
results += '<h2>Currently ' + json.main.temp + " °F</h2>";
results += "<p>";
for (let i = 0; i < json.weather.length; i++) {
results += json.weather[i].description;
if (i !== json.weather.length - 1) {
results += ", ";
}
}
results += "</p>";
results += "<hr>";
results += "<ul class='force-bullet'><li>Feels like: " + json.main.feels_like +
" °F</li><li>High: " + json.main.temp_max + " °F</li><li>Low: " +
json.main.temp_min + "°F</li><li>Sunrise: " + moment(json.sys.sunrise * 1000).format('h:mm a') +
"</li><li>Sunset: " + moment(json.sys.sunset * 1000).format('h:mm a') + "</li><li>Wind: " + json.wind.speed + " mph</li></ul></div>";
document.getElementById("weatherResults").innerHTML = results;
}).catch(error => {
console.log(error);
});
// GET FORECASTED WEATHER IN GIVEN CITY (NAME)
const url2 = "http://api.openweathermap.org/data/2.5/forecast?q=" + value + ", US&units=imperial" + "&APPID=6cb68e866fb8a28497eb7120da1d1ba2";
fetch(url2)
.then(response => {
return response.json();
}).then(json => {
console.log(json);
let forecast = "";
for (let i = 0; i < json.list.length; i++) {
if (i % 8 == 0) {
forecast += "<div class='threeday signup-header2'>";
}
forecast += "<h2>" + moment(json.list[i].dt_txt).format('MMMM Do, h:mm a') + "</h2>";
forecast += "<p>" + json.list[i].main.temp + " °F, " + json.list[i].weather[0].description + "</p>";
forecast += '<img src="http://openweathermap.org/img/w/' + json.list[i].weather[0].icon + '.png"/>';
forecast += "<hr>";
forecast += "<ul class='force-bullet'><li>Feels like: " + json.list[i].main.feels_like +
" °F</li><li>3-hr High: " + json.list[i].main.temp_max + " °F</li><li>3-hr Low: " +
json.list[i].main.temp_min + "</li><li>Wind: " + json.list[i].wind.speed + " mph</li></ul>";
if (i % 8 == 7) {
forecast += "</div>";
}
}
document.getElementById("forecastResults").innerHTML = forecast;
});
});