forked from oscarluther/weather-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
57 lines (46 loc) · 1.99 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
const weather = new Weather();
const storage = new Storage();
const locationData = storage.getStorage();
document.addEventListener("DOMContentLoaded", getWeather(locationData.city, locationData.country));
document.querySelector("#change-weather").addEventListener("click", changeLocation);
function changeLocation() {
let country = document.querySelector("#country").value || "India";
let city = document.querySelector("#city").value || "Delhi";
console.log("App.js values picked", { country, city });
storage.saveStorage(city, country);
getWeather(city, country);
$('#locationModal').modal('hide');
}
function getWeather(city, country) {
weather.getWeather(city, country)
.then(res => {
display(res);
})
.catch(error => console.log(error));
}
function display(weatherData) {
const location = document.getElementById("location");
const temperature = document.getElementById("temperature");
const description = document.getElementById("description");
const humidity = document.getElementById("humidity");
const feelsLike = document.getElementById("feelsLike");
description.textContent = titleCase(weatherData.weather[0].description);
location.textContent = weatherData.name;
temperature.textContent = `Temperature: ${toCelsius(weatherData.main.temp)}`;
humidity.textContent = `Humidity: ${weatherData.main.humidity}%`;
feelsLike.textContent = `Feels Like: ${toCelsius(weatherData.main.feels_like)}`;
}
titleCase("HELLO tesTing sTuff");
function titleCase(text) {
console.log("BEFORE titleCase:", text);
let result = text.split(" ")
.map(x => { return (x.charAt(0).toUpperCase() + x.slice(1).toLowerCase()) }) // converts all words to pascal case
.reduce((acc, item) => { //combines all the words into a string
return acc + " " + item;
});
console.log("AFTER titleCase:", text);
return result;
}
function toCelsius(kelvin) {
return (kelvin - 273.15).toFixed(2);
}