-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
92 lines (77 loc) · 2.18 KB
/
app.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//SELECT ELEMENTS
const icon = document.getElementById('icon');
const temp = document.getElementById('temp');
const desc = document.getElementById('desc');
const date = document.getElementById('date');
const time = document.getElementById('time');
const city = document.getElementById('city');
//APP DATA
const weather = {};
weather.temperature = {
unit: "celsius"
}
const KELVIN = 273;
//GET WEATHER
function getWeather() {
let api = 'https://api.openweathermap.org/data/2.5/weather?id=3333229&appid=30b3668879133b2360e1b3e775d4418f';
console.log(api);
fetch(api)
.then(function(response){
let data = response.json();
return data;
})
.then(function(data){
weather.temperature.value = Math.floor(data.main.temp - KELVIN);
weather.description = data.weather[0].description;
weather.icon = data.weather[0].icon;
})
.then(function(){
displayWeather();
displayTime();
displayDate();
});
}
function displayWeather() {
temp.textContent = weather.temperature.value + '\u00B0';
desc.textContent = weather.description;
icon.src = 'img/' + weather.icon + '.svg';
}
function displayTime() {
let d = new Date();
let hour = d.getHours();
let minute = d.getMinutes();
checkTime(hour);
checkTime(minute);
let output = hour + ':' + minute;
time.textContent = output;
setTimeout(displayTime, 60000);
}
function displayDate() {
let d = new Date();
let day = d.getDay();
const months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
let month = months[d.getMonth()];
day = checkDay(day);
let output = day + " " + month;
date.textContent = output;
}
function checkDay(d) {
if(d === 1) {
d += "st";
} else if (d === 2) {
d += "nd";
} else if (d === 3) {
d += "rd";
} else {
d += "th";
}
return d;
}
function checkTime(i) {
if(i<10) {
i = "0" + i;
}
return i;
}
// run the funtion on load
document.addEventListener('DOMContentLoaded', getWeather);