-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
50 lines (40 loc) · 1.58 KB
/
main.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
const api = {
key: "cd1ed65a23904995903bba3dd56e09fc",
base: "https://api.openweathermap.org/data/2.5/"
}
const searchbox = document.querySelector('.search-box');
searchbox.addEventListener('keypress', setQuery);
function setQuery(evt) {
if(evt.keyCode == 13) {
getResult(searchbox.value);
}
}
function getResult(query) {
fetch(`${api.base}weather?q=${query}&units=metric&APPID=${api.key}`)
.then(weather => {
return weather.json();
}).then(displayResult);
}
function displayResult(weather) {
console.log(weather);
//Get city info
let city = document.querySelector('.location .city');
city.innerText = (`${weather.name}, ${weather.sys.country}`)
//Get date
let today = new Date();
let date ='Current Date: ' +today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
let dateDisplay = document.querySelector('.location .date');
dateDisplay.innerText = `${date}`;
//Get current temp
let currentTemp = document.querySelector('.current .temp');
currentTemp.innerHTML = `${Math.round(weather.main.temp)}<span>°C</span>`;
//Get current weather
const weather_el = document.querySelector('.current .weather');
weather_el.innerText = weather.weather[0].main;
//Get current feeling
const feels = document.querySelector('.current .feels');
feels.innerText = `Feels Like: ${weather.main.feels_like}°c`;
//Get high low temperature
const hilow = document.querySelector('.current .hi-low');
hilow.innerText = `Max: ${weather.main.temp_max}°c, Min: ${weather.main.temp_min}°c`;
}