forked from econway0321/project-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather_logicw.js
52 lines (40 loc) · 1.8 KB
/
weather_logicw.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
var api = {
key: 'f3ad52969d96a88aec01f1b67be1643a', //need to change api key
baseurl: 'https://api.openweathermap.org/data/2.5/'
};
var searchbox = document.querySelector('.search-box');
searchbox.addEventListener('keypress', setQuery)
function setQuery(evt) {
if (evt.keyCode == 13) { //13 = Enter key is where the event listener is attached
getResults(searchbox.value);
};
};
function getResults (query) { //here we're accessing openweathermap API
fetch(`${api.baseurl}weather?q=${query}&units=imperial&APPID=${api.key}`)
.then(weather => {
return weather.json();
}).then(displayResults);
};
function displayResults (weather) { //json results are displayed in console
var city = document.querySelector('.location .city');
city.innerText = `${weather.name}, ${weather.sys.country}`;
var now = new Date();
var date = document.querySelector('.location .date');
date.innerText = dateBuilder(now);
var temp = document.querySelector('.current .temp');
temp.innerHTML = `${Math.round(weather.main.temp)}<span>°f</span>`;
var weather_cur = document.querySelector('.current .weather');
weather_cur.innerText = weather.weather[0].main;
var hilow = document.querySelector('.hi-low');
hilow.innerText = `${Math.round(weather.main.temp_min)}°F / ${Math.round(weather.main.temp_max)}°F`
}
function dateBuilder (d) {
var months = ["January","February","March","April","May","June","July",
"August","September","October","November","December"];
var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var day = days[d.getDay()];
var date = d.getDate();
var month = months[d.getMonth()];
var year = d.getFullYear();
return `${day} ${date} ${month} ${year}`;
}