-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
67 lines (61 loc) · 1.65 KB
/
Copy pathindex.js
File metadata and controls
67 lines (61 loc) · 1.65 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
const api = {
key:'76e4926a53984f234ca3f7dd3b3c17b3',
baseurl: 'https://api.openweathermap.org/data/2.5/'
}
const searchBox = document.querySelector(".search-box");
searchBox.addEventListener('keypress',setQuery )
function setQuery(e){
if(e.keyCode==13){
console.log(searchBox.value);
getResults(searchBox.value)
}
}
function getResults(query){
fetch(`${api.baseurl}weather?q=${query}&units=metric&APPID=${api.key}`)
.then((weather)=>{
return weather.json();
})
.then(displayresults)
}
function displayresults(weather){
console.log(weather);
let city = document.querySelector('.location .city'),
date = document.querySelector('.location .date'),
temp = document.querySelector(".temp"),
description = document.querySelector(".weather");
const now = new Date();
city.innerHTML = `${weather.name}, ${weather.sys.country}`
date.innerHTML=dataBuilder(now)
temp.innerHTML = `${Math.round(weather.main.temp)}<span>°C</span>`
description.innerHTML = `${weather.weather[0].main}`
}
function dataBuilder(s){
let months = [
"Januar",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
],
days = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
]
let weekDay = days[s.getDay()]
let date = s.getDate()
let month = months[s.getMonth()]
let year = s.getFullYear()
return `${weekDay} ${date} ${month} ${year}`
}