-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
133 lines (83 loc) · 3 KB
/
script.js
File metadata and controls
133 lines (83 loc) · 3 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
const weatherForm = document.querySelector(".weatherform");
const cityInput = document.querySelector(".city-input");
const card = document.querySelector(".card");
const apikey = "65ebdb7ab58cb4e9a2dc7d21c1b28351";
weatherForm.addEventListener("submit", async event=>{
event.preventDefault();
const city = cityInput.value;
if(city){
try{
const weatherData = await getWeatherData(city);
displayWeatherinfo(weatherData);
}
catch(error){
console.log(error);
displayError(error);
}
} else{
displayError("Please enter the City !");
}
});
async function getWeatherData(city){
const apiurl = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apikey}&units=metric
`;
const response = await fetch(apiurl);
console.log(response);
if(!response.ok){
throw new Error("Could not fetch weather data !");
}
return await response.json();
}
function displayWeatherinfo(data){
const {name: city, main:{temp, humidity}, weather:[{description,id}]} = data;
card.textContent = "";
card.style.display = "flex";
const cityDisplay = document.createElement("h1");
const tempDisplay = document.createElement("p");
const humidityDisplay = document.createElement("p");
const descDisplay = document.createElement("p");
const weatherEmoji = document.createElement("p");
cityDisplay.textContent = city;
tempDisplay.textContent = `${temp}°F`;
humidityDisplay.textContent = `Humidity ${humidity}%`;
descDisplay.textContent = description;
weatherEmoji.textContent = getWeatherEmoji(id);
cityDisplay.classList.add("cityDisplay");
tempDisplay.classList.add("tempDisplay");
humidityDisplay.classList.add("humidityDisplay");
descDisplay.classList.add("descdisplay");
weatherEmoji.classList.add("weatherEmoji");
card.appendChild(cityDisplay);
card.appendChild(tempDisplay);
card.appendChild(humidityDisplay);
card.appendChild(descDisplay);
card.appendChild(weatherEmoji);
}
function getWeatherEmoji(weatherId){
switch(true){
case(weatherId >= 200 && weatherId < 300):
return "🌧️";
case(weatherId >= 300 && weatherId < 400):
return "⛈️";
case(weatherId >= 500 && weatherId < 600):
return "⛈️";
case(weatherId >= 600 && weatherId < 700):
return "❄️";
case(weatherId >= 700 && weatherId < 800):
return "🌨️";
case(weatherId ===800):
return "☀️";
case(weatherId >= 800 && weatherId < 810):
return "☁️";
default :
return "❓";
}
}
function displayError(message){
const errorDisplay = document.createElement("p");
errorDisplay.textContent = message;
errorDisplay.classList.add("errorDisplay");
card.textContent = "";
card.style.display ="flex";
card.appendChild(errorDisplay);
}