-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathscripts.js
104 lines (82 loc) · 3.11 KB
/
scripts.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
93
94
95
96
97
98
99
100
101
102
103
104
const unit = document.querySelector(".unitsign").innerHTML
const unitbtn = document.querySelector(".unitsign")
//set the date
const date = new Date().toDateString()
document.querySelector('.datee').innerHTML = date;
//get current weather from api
function getcurrentweather(city){
fetch("https://api.openweathermap.org/data/2.5/weather?q=" + city
+"&units=metric&appid=" + this.apiKey
)
.then((response)=>response.json())
.then((todaydata)=>
this.displayWeather(todaydata)
)
}
//display current weather
function weathertoday(todaydata){
const { name } = todaydata;
const {country} = todaydata.sys;
const { speed } = todaydata.wind;
let {temp, humidity, pressure} = todaydata.main;
const {icon, description} = todaydata.weather[0];
document.querySelector(".cityname").innerHTML = name +" ," +country
document.querySelector(".wind").innerHTML = "Wind: " + speed + "m/s"
document.querySelector(".humidity").innerHTML = "Humidity: " + humidity + "%"
document.querySelector(".pressure").innerHTML = "Pressure: " + pressure +"hPa"
document.querySelector('.icon').src="https://openweathermap.org/img/wn/"+ icon +".png"
document.querySelector(".description").innerHTML = description
document.querySelector(".spantemp").innerHTML= temp
document.querySelector("#remove").classList.remove("loading")
}
let weather= {
"apiKey": "e1bc1b61d865e5fde2987fd8aacecfad",
fetchWeather: getcurrentweather,
displayWeather: function(todaydata){
weathertoday(todaydata)
},
search: function(){
this.fetchWeather(document.querySelector(".inputsearch").value)
}
}
//function to change to fahrenheit
function tempconversion(unit, arg){
let tempbutton = document.querySelector(".spantemp")
let tempc = Math.round(temp)
let tempf = Math.round(temp* (9/5)+32)
if(unit === "°C"){
tempbutton.innerHTML = tempc
}else{
tempbutton.innerHTML = tempf
}
}
//eventlisteners
const searchbutton =document.querySelector("#buttong")
searchbutton.addEventListener("click", function(e){
document.querySelector(".checkbox").checked = false
unitbtn.innerHTML = "°C"
weather.search()
e.preventDefault()
document.querySelector(".inputsearch").value = ""
})
document.querySelector(".inputsearch").addEventListener("keyup", function(e){
if(e.key =="Enter"){
document.querySelector(".checkbox").checked = false
unitbtn.innerHTML = "°C"
weather.search()
}
})
document.querySelector(".checkbox").addEventListener("change", function(e){
const temp = document.querySelector(".spantemp").innerHTML
let tempbutton = document.querySelector(".spantemp")
let tempc = ((temp-32)*5/9).toFixed(2)
let tempf = (temp* (9/5)+32).toFixed(2)
if(unitbtn.innerHTML === "°F"){
unitbtn.innerHTML = "°C"
tempbutton.innerHTML = tempc
}else{
unitbtn.innerHTML = "°F"
tempbutton.innerHTML = tempf
}
})
weather.fetchWeather("Lagos");