-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
99 lines (85 loc) · 2.92 KB
/
scripts.js
File metadata and controls
99 lines (85 loc) · 2.92 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
//weather app
const weatherForm = document.querySelector(".weatherForm");
const cityInput = document.querySelector(".cityInput");
const card = document.querySelector(".card");
const apiKey = "e2d9cabe5e03e01ddbda0fb217f3b1a0";
weatherForm.addEventListener("submit", async (event) => {
event.preventDefault();
const city = cityInput.value;
if (city) {
try {
const weatherData = await getWeatherData(city);
displayWeatherInfo(weatherData);
} catch (error) {
console.error(error);
displayError(error);
}
} else {
displayError("Please Enter a city!");
}
});
async function getWeatherData(city) {
const apiUrl = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}`;
const response = await fetch(apiUrl);
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 - 273.15).toFixed(1)}°C`; //in case of farenheit tem ${((temp - 273.15)* (9/5) + 32).toFixed(1)}°C
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 "⛈\uFE0F";
case weatherId >= 300 && weatherId < 400:
return "🌧\uFE0F";
case weatherId >= 500 && weatherId < 600:
return "🌨\uFE0F";
case weatherId >= 600 && weatherId < 700:
return "❄\uFE0F";
case weatherId >= 700 && weatherId < 800:
return "🌫\uFE0F";
case weatherId === 800:
return "☀\uFE0F";
case weatherId >= 801 && weatherId < 810:
return "☁\uFE0F";
default:
return "❓\uFE0F";
} //\uFE0F is used to force the respected colors of emojis
}
function displayError(message) {
const errorDisplay = document.createElement("p");
errorDisplay.textContent = message;
errorDisplay.classList.add("errorDisplay");
card.textContent = "";
card.style.display = "flex";
card.appendChild(errorDisplay);
}