forked from ComputerScienceSoceityNITS/weatherwatch24
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
61 lines (52 loc) · 2.15 KB
/
index.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
const API_KEY = `3265874a2c77ae4a04bb96236a642d2f`
const form = document.querySelector("form")
const inputbox = document.querySelector("#inputbox")
const weather = document.querySelector(".weather-ui")
const getWeather = async(city) => {
weather.innerHTML = `<h2> Loading... <h2>`
const url = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${API_KEY}&units=metric`
const response = await fetch(url);
const data = await response.json()
return showWeather(data)
}
const showWeather = (data) => {
if (data.cod == "404") {
weather.innerHTML = `<h2> City Not Found <h2>`
return;
}
if(data.weather[0].main == "Rain")
document.body.style.background = " url('rainy.jpg')";
if(data.weather[0].main == "Drizzle")
document.body.style.background = " url('rainy.jpg')";
if(data.weather[0].main == "Clouds"){
document.body.style.background = " url('Cloudy.jpg')";
}
if(data.weather[0].main == "Smoke")
document.body.style.background = " url('hazenew.jpg')";
if(data.weather[0].main == "Haze")
document.body.style.background = " url('hazenew.jpg')";
if(data.weather[0].main == "Mist")
document.body.style.background = " url('hazenew.jpg')";
if(data.weather[0].main == "Fog")
document.body.style.background = " url('hazenew.jpg')";
if(data.weather[0].main == "Clear")
document.body.style.background = " url('clearsky.jpg')";
if(data.weather[0].main == "Thunderstorm")
document.body.style.background = " url('thunder.jpg')";
if(data.weather[0].main == "Dust")
document.body.style.background = " url('sandy.jpg')";
if(data.weather[0].main == "Sand")
document.body.style.background = " url('sandy.jpg')";
if(data.weather[0].main == "Snow")
document.body.style.background = " url('snowy.jpg')";
weather.innerHTML = `
<p>${data.main.temp}℃<br>${data.weather[0].main}<br>${data.main.temp_min}℃(min) ${data.main.temp_max}℃(max)</p>
`
}
form.addEventListener(
"submit",
function(event) {
getWeather(inputbox.value)
event.preventDefault();
}
)