-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
40 lines (35 loc) · 1.53 KB
/
script.js
File metadata and controls
40 lines (35 loc) · 1.53 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
const key = config.API_KEY;
const baseURL = 'https://api.openweathermap.org/data/2.5/'
const search = document.querySelector("#search");
search.addEventListener('keypress', setQuery);
const location_el = document.querySelector("#location");
const temperature_el = document.querySelector("#temperature");
const date_el = document.querySelector("#date");
const weather_el = document.querySelector("#weather");
const min_max_el = document.querySelector("#min-max");
const image = document.querySelector("#weather_icon");
const metric = "ºC";
const months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
function setQuery (evt) {
if(evt.keyCode == 13){
console.log(search.value);
getResults(search.value);
}
}
function getResults (query) {
fetch(`${baseURL}weather?q=${query}&units=metric&APPID=${key}`)
.then(weather => {
return weather.json();
}).then(showResults)
}
function showResults (weather) {
console.log(weather);
var d = new Date();
location_el.innerHTML = weather.name + ", " + weather.sys.country;
temperature_el.innerHTML = Math.round(weather.main.temp) + metric;
date_el.innerHTML = `${months[d.getMonth()]} ${d.getDate()}, ${d.getFullYear()} `;
weather_el.innerHTML = weather.weather[0].main;
min_max_el.innerHTML = Math.round(weather.main.temp_min) + metric + "/" +
Math.round(weather.main.temp_max) + metric;
image.src = `http://openweathermap.org/img/wn/${weather.weather[0].icon}.png`;
}