-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
76 lines (69 loc) · 2.59 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
63
64
65
66
67
68
69
70
71
72
73
74
const inputBox = document.querySelector('.input-box');
const searchBtn = document.getElementById('searchBtn');
const weather_Img = document.querySelector('.weather-img');
const temperature = document.querySelector('.temprature');
const description = document.querySelector('.description');
const humidity = document.getElementById('humidity');
const wind_speed = document.getElementById('wind-speed');
const location_not_found = document.querySelector('.location-not-found');
const weather_body = document.querySelector('.weather-body');
const checkweather=async (city)=>{
const api="c6bc976394f13845dc637ae1be0ff583";
const url=`https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${api}`;
const weather_data=await fetch(url);
try{
const data=await weather_data.json();
console.log(data);
if(data.cod=="404"){
console.error("City not found");
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(data.main.temp-273.15)}°C`;
description.innerHTML=`${data.weather[0].description}`;
humidity.innerHTML=`${data.main.humidity}%`;
wind_speed.innerHTML=`${Math.round(data.wind.speed)} km/h`;
const weather=data.weather[0].main;
if(weather=="Clouds"){
weather_Img.src="./assets/cloud.png";
}else if(weather=="Rain"){
weather_Img.src="./assets/rain.png";
}
else if(weather=="Clear"){
weather_Img.src="./assets/clear.png";
}else if(weather=="Haze"){
weather_Img.src="./assets/mist.png";
}else if(weather=="Mist"){
weather_Img.src="./assets/mist.png";
}
else if(weather=="Snow"){
weather_Img.src="./assets/snow.png";
}
else if(weather=="Thunderstorm"){
weather_Img.src="./assets/rain.png";
}
}catch(e){
console.error("City not found");
}
}
inputBox.addEventListener("keydown", (e) => {
if (e.key === "Enter") {
const city = inputBox.value.trim();
if (city) {
checkweather(city);
} else {
alert("Please enter a city name."); // Alert if the input is empty
}
}
});
searchBtn.addEventListener('click', () => {
const city = inputBox.value.trim();
if (city) {
checkweather(city);
} else {
alert("Please enter a city name."); // Alert if the input is empty
}
});