-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
63 lines (45 loc) · 1.95 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
const inputBox = document.querySelector('.input-box');
const searchBtn = document.getElementById('searchBtn');
const weatherimg = document.querySelector('.weather-img');
const temperature = document.querySelector('.temperature');
const description = document.querySelector('.description');
const humidity = document.getElementById('humidity');
const windspeed = document.getElementById('wind-speed');
const location_not_found = document.querySelector('.location-not-found');
const weather_body = document.querySelector('.weather-body');
async function checkweather(city){
const api_key="3746601e3f5c702120cecbe69120dd1c";
const url = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${api_key}`;
const weather_data =await fetch(`${url}`).then(response =>response.json());
if(weather_data.cod ==='404'){
location_not_found.style.display = "flex";
weather_body.style.display = "none";
return;
}
location_not_found.style.display = "none";
weather_body.style.display = "flex";
temperature.innerHTML =`${Math.round(weather_data.main.temp - 273.15)}°C`;
description.innerHTML =`${weather_data.weather[0].description}`;
humidity.innerHTML =`${weather_data.main.humidity}%`;
windspeed.innerHTML =`${weather_data.wind.speed}Km/H`;
switch(weather_data.weather[0].main){
case 'Clouds':
weatherimg.src="./images/icons8-cloud-94.png";
break;
case 'Clear':
weatherimg.src="./images/icons8-clear-sky-64.png";
break;
case 'Rain':
weatherimg.src="./images/icons8-rain-48.png";
break;
case 'Mist':
weatherimg.src="./images/icons8-mist-100.png";
break;
case 'Snow':
weatherimg.src="./images/icons8-snow-96.png";
break;
}
}
searchBtn.addEventListener('click',()=>{
checkweather(inputBox.value);
});