Skip to content

Commit 43da0c2

Browse files
Update app.js
1 parent 736e9fe commit 43da0c2

File tree

1 file changed

+40
-46
lines changed

1 file changed

+40
-46
lines changed

app.js

Lines changed: 40 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,94 @@
1+
// Tutorial by http://youtube.com/CodeExplained
2+
// api key : 82005d27a116c2880c8f0fcb866998a0
3+
14
// SELECT ELEMENTS
25
const iconElement = document.querySelector(".weather-icon");
36
const tempElement = document.querySelector(".temperature-value p");
47
const descElement = document.querySelector(".temperature-description p");
58
const locationElement = document.querySelector(".location p");
69
const notificationElement = document.querySelector(".notification");
710

8-
// OUR APPLICATION DATA
11+
// App data
912
const weather = {};
1013

1114
weather.temperature = {
1215
unit : "celsius"
13-
};
16+
}
1417

15-
// APP CONSTANTS AND VARS
18+
// APP CONSTS AND VARS
1619
const KELVIN = 273;
17-
1820
// API KEY
1921
const key = "82005d27a116c2880c8f0fcb866998a0";
2022

21-
// CHECK IF BROWSER SUPPORT GEOLOCATION and GET LOCATION
22-
if("geolocation" in navigator){
23+
// CHECK IF BROWSER SUPPORTS GEOLOCATION
24+
if('geolocation' in navigator){
2325
navigator.geolocation.getCurrentPosition(setPosition, showError);
24-
2526
}else{
2627
notificationElement.style.display = "block";
27-
notificationElement.innerHTML = "<p>Browser Doesn't Support Geolocation.</p>";
28+
notificationElement.innerHTML = "<p>Browser doesn't Support Geolocation</p>";
2829
}
2930

30-
// SET POSITION
31+
// SET USER'S POSITION
3132
function setPosition(position){
32-
console.log(position);
3333
let latitude = position.coords.latitude;
3434
let longitude = position.coords.longitude;
3535

3636
getWeather(latitude, longitude);
3737
}
3838

39-
// GET WEATHER FROM API
39+
// SHOW ERROR WHEN THERE IS AN ISSUE WITH GEOLOCATION SERVICE
40+
function showError(error){
41+
notificationElement.style.display = "block";
42+
notificationElement.innerHTML = `<p> ${error.message} </p>`;
43+
}
44+
45+
// GET WEATHER FROM API PROVIDER
4046
function getWeather(latitude, longitude){
4147
let api = `http://api.openweathermap.org/data/2.5/weather?lat=${latitude}&lon=${longitude}&appid=${key}`;
48+
4249
fetch(api)
4350
.then(function(response){
4451
let data = response.json();
4552
return data;
46-
47-
}).then(function(data){
48-
weather.temperature.value = Math.floor(data.main.temp - KELVIN); // Celsius
53+
})
54+
.then(function(data){
55+
weather.temperature.value = Math.floor(data.main.temp - KELVIN);
4956
weather.description = data.weather[0].description;
5057
weather.iconId = data.weather[0].icon;
5158
weather.city = data.name;
5259
weather.country = data.sys.country;
53-
}).then(function(){
60+
})
61+
.then(function(){
5462
displayWeather();
5563
});
5664
}
5765

58-
// SHOW ERROR
59-
function showError(error) {
60-
notificationElement.style.display = "block";
61-
notificationElement.innerHTML = `<p> ${error.message} </p>`;
66+
// DISPLAY WEATHER TO UI
67+
function displayWeather(){
68+
iconElement.innerHTML = `<img src="icons/${weather.iconId}.png"/>`;
69+
tempElement.innerHTML = `${weather.temperature.value}°<span>C</span>`;
70+
descElement.innerHTML = weather.description;
71+
locationElement.innerHTML = `${weather.city}, ${weather.country}`;
72+
}
73+
74+
// C to F conversion
75+
function celsiusToFahrenheit(temperature){
76+
return (temperature * 9/5) + 32;
6277
}
6378

64-
// toggle fahrenheit ans celcius
79+
// WHEN THE USER CLICKS ON THE TEMPERATURE ELEMENET
6580
tempElement.addEventListener("click", function(){
6681
if(weather.temperature.value === undefined) return;
67-
if(weather.temperature.unit === "celsius"){
68-
82+
83+
if(weather.temperature.unit == "celsius"){
6984
let fahrenheit = celsiusToFahrenheit(weather.temperature.value);
7085
fahrenheit = Math.floor(fahrenheit);
7186

72-
tempElement.innerHTML = `${fahrenheit}° <span>F</span>`;
73-
weather.temperature.unit = "fahrenheit"
87+
tempElement.innerHTML = `${fahrenheit}°<span>C</span>`;
88+
weather.temperature.unit = "fahrenheit";
7489
}else{
75-
tempElement.innerHTML = `${weather.temperature.value}° <span>F</span>`;
90+
tempElement.innerHTML = `${weather.temperature.value}°<span>C</span>`;
7691
weather.temperature.unit = "celsius"
7792
}
7893
});
7994

80-
// display weather info to ui
81-
function displayWeather(){
82-
83-
iconElement.innerHTML = `<img src="icons/${weather.iconId}.png"/>`;
84-
tempElement.innerHTML = `${weather.temperature.value}° <span>C</span>`;
85-
descElement.innerHTML = weather.description;
86-
locationElement.innerHTML = `${weather.city}, ${weather.country}`;
87-
}
88-
89-
// °C to °F conversion
90-
function celsiusToFahrenheit(temperature){
91-
// (0 °C × 9/5) + 32 = 32 °F
92-
return (temperature * 9/5) + 32;
93-
}
94-
95-
96-
97-
98-
99-
100-

0 commit comments

Comments
 (0)