-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
62 lines (51 loc) · 2.64 KB
/
script.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
const searchBox = document.querySelector(".search input");
const searchBtn = document.querySelector(".search button");
const weatherIcon = document.querySelector(".weather-icon");
function checkWeather(city) {
fetch("https://api.weatherapi.com/v1/current.json?key=9fc2e5b3947f4f46b5a94210241007&q=" + city)
.then(res=> res.json())
.then(data => {
document.querySelector(".city").innerHTML = data.location.name + "," + " " + data.location.country;
document.querySelector(".forecast").innerHTML = data.current.condition.text;
document.querySelector(".temp").innerHTML = Math.round(data.current.temp_c) + "°C";
document.querySelector(".date").innerHTML = data.location.localtime;
document.querySelector(".feels").innerHTML = data.current.feelslike_c + "°C";
document.querySelector(".humidity").innerHTML = data.current.humidity + "%" ;
document.querySelector(".wind").innerHTML = data.current.wind_kph + " kph ";
let condition = data.current.condition.text.toLowerCase();
if(condition.includes("mist")){
weatherIcon.src = "images/mist.png";
}
else if(condition.includes("drizzle")){
weatherIcon.src = "images/drizzle.png";
}
else if(condition.includes("rain") || condition.includes("shower")){
weatherIcon.src = "images/rain.png";
}
else if(condition.includes("thunder") || condition.includes("lightning")){
weatherIcon.src = "images/thunderstrom.png";
}
else if(condition.includes("cloudy") || condition.includes("overcast")){
weatherIcon.src = "images/cloudy.png";
}
else if(condition.includes("sunny") || condition.includes("clear")){
weatherIcon.src = "images/sun.png";
}
else if(condition.includes("snow")){
weatherIcon.src = "images/snowy.png";
}
else if(condition.includes("windy")){
weatherIcon.src = "images/wind.png";
}
else if(condition.includes("fog")){
weatherIcon.src = "images/fog.png";
}
else {
weatherIcon.src = "images/cloudy.png"
}
document.querySelector(".weather").style.display = "block";
});
}
searchBtn.addEventListener("click", ()=>{
checkWeather(searchBox.value);
})