-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
57 lines (50 loc) · 1.81 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
window.addEventListener('load', ()=> {
let long;
let lat;
let temperatureDescription = document.querySelector('.temperature-description');
let temperatureDegree = document.querySelector('.temperature-degree');
let locationTimezone = document.querySelector('.location-timezone');
let degreeSection = document.querySelector('.degree-section');
const degreeSpan = document.querySelector('.degree-section span');
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(position => {
long = position.coords.longitude;
lat = position.coords.latitude;
const api = `https://cors-anywhere.herokuapp.com/https://api.darksky.net/forecast/b5f826fc2f61339c095834ed4a63ee16/${lat},${long}`;
fetch(api)
.then(response => {
return response.json();
})
.then(data => {
const { temperature, summary, icon } = data.currently;
//Set DOM Elements from the API
temperatureDegree.textContent = temperature;
temperatureDescription.textContent = summary;
locationTimezone.textContent = data.timezone;
//Formula for degree conversion
let celcius = (temperature - 32) * (5 / 9);
//Set Icon
setIcons(icon, document.querySelector(".icon"));
//Change temperature to Celcius/Farenheit
degreeSection.addEventListener('click', () => {
if(degreeSpan.textContent == "F") {
degreeSpan.textContent= "C";
temperatureDegree.textContent = strip(celcius);
}else{
degreeSpan.textContent = "F"
temperatureDegree.textContent = temperature;
}
})
});
});
}
function setIcons(icon, iconID) {
const skycons = new Skycons({color: "white"});
const currentIcon = icon.replace(/-/g, "_").toUpperCase();
skycons.play();
return skycons.set(iconID, Skycons[currentIcon]);
}
function strip(number) {
return (parseFloat(number).toPrecision(4))
}
});