-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
62 lines (53 loc) · 2.86 KB
/
Copy pathindex.js
File metadata and controls
62 lines (53 loc) · 2.86 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
let weather = {
apiKey:"4efed280b0fcf3f86060f291be2c67c4",
fetchWeather: function(city){
fetch("https://api.openweathermap.org/data/2.5/weather?q="+city+"&units=metric&appid="+this.apiKey).then((response) => response.json()).then((data) => this.displayWeather(data));
},
fetchCords: function(lat, lon){
fetch("https://api.openweathermap.org/data/2.5/weather?lat="+lat+"&lon="+lon+"&units=metric&appid="+this.apiKey).then((response) => response.json()).then((data) => this.switchCordToCity(data) & this.geolocationToCCA2(data));
},
fetchCountry: function(city){
fetch("https://restcountries.com/v3.1/capital/"+city+"").then((response) => response.json()).then((data) => this.displayCountry(data));
},
geolocationToCCA2: function(data){
const { country } = data.sys;
console.log(country);
fetch("https://restcountries.com/v3.1/alpha/"+country+"").then((response) => response.json()).then((data) => this.geolocationToCountry(data));
},
switchCordToCity: function(data){
const { name } = data;
city = name;
this.fetchWeather(city);
},
degToCompass: function(num){
var val = Math.floor((num / 22.5) + 0.5);
var arr = ["N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"];
direction = arr[(val % 16)];
return direction;
},
displayWeather: function(data){
const { name } = data;
const {description,main } = data.weather[0];
const { temp, temp_min, temp_max, pressure, feels_like, humidity} = data.main;
const {speed, deg } = data.wind;
console.log(name,description,main,temp, temp_min, temp_max, pressure, feels_like, humidity,speed,deg)
document.querySelector(".city").innerText =name;
document.querySelector(".temp").innerText = temp + "°C";
document.querySelector(".description").innerText = "Description: "+ description;
document.querySelector(".temp-min-max").innerText = "Minimum : " + temp_min + "°C / Maximum: " + temp_max + "°C";
document.querySelector(".feels-like").innerText = "Feels like: " + feels_like + "°C";
document.querySelector(".humidity").innerText ="Humidity: " + humidity + "%";
document.querySelector(".pressure").innerText = "Pressure: " + pressure + "mbar";
document.querySelector(".wind").innerText ="Wind speed: " + speed + "m/s";
document.querySelector(".wind-direction").innerText = "Wind Direction: " + deg + "° (" + this.degToCompass(deg) + ")";
},
search: function (){
this.fetchWeather(document.querySelector(".search-bar").value);
},
};
document.querySelector(".search-bar").addEventListener("keyup", function(event){
if (event.key == "Enter"){
weather.search();
}
});
weather.fetchWeather("America");