-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
93 lines (82 loc) · 3.52 KB
/
script.js
File metadata and controls
93 lines (82 loc) · 3.52 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
const apiKey = "0fb63a4e5974f6c01a7d49fe2ea382b8";
const apiUrl = "https://api.openweathermap.org/data/2.5/weather?units=metric&q=";
const forecastApiUrl = "https://api.openweathermap.org/data/2.5/forecast?units=metric&q=";
const searchBox = document.querySelector("#locationInput");
const searchBtn = document.querySelector("#searchBtn");
const weatherIcon = document.querySelector(".weather-icon");
const errorDisplay = document.querySelector(".error");
const weatherDisplay = document.querySelector(".weather");
const cityDisplay = document.querySelector(".city");
const humidityDisplay = document.querySelector(".humidity");
const windDisplay = document.querySelector(".wind");
const forecastDaysDisplay = document.querySelector(".forecast-days");
async function fetchWeatherData(city) {
try {
const response = await fetch(apiUrl + city + `&appid=${apiKey}`);
const data = await response.json();
return data;
} catch (error) {
console.error("Error fetching weather data:", error);
return null;
}
}
async function fetchForecastData(city) {
try {
const response = await fetch(forecastApiUrl + city + `&appid=${apiKey}`);
const data = await response.json();
return data.list;
} catch (error) {
console.error("Error fetching forecast data:", error);
return null;
}
}
function displayWeatherData(weatherData) {
if (!weatherData) {
errorDisplay.style.display = "block";
weatherDisplay.style.display = "none";
return;
}
errorDisplay.style.display = "none";
weatherDisplay.style.display = "block";
cityDisplay.textContent = weatherData.name;
humidityDisplay.textContent = `Humidity: ${weatherData.main.humidity}%`;
windDisplay.textContent = `Wind Speed: ${weatherData.wind.speed}km/h`;
const temperature = Math.round(weatherData.main.temp);
document.querySelector('.temp').textContent = `${temperature}°C`;
const weatherIconUrl = `https://openweathermap.org/img/wn/${weatherData.weather[0].icon}.png`;
weatherIcon.src = weatherIconUrl;
}
function displayForecastData(forecastData) {
forecastDaysDisplay.innerHTML = "";
for (let i = 0; i < forecastData.length; i += 8) {
const forecastItem = forecastData[i];
const forecastDate = new Date(forecastItem.dt * 1000);
const forecastDay = forecastDate.toLocaleDateString("en-US", { weekday: "short" });
const forecastTemp = Math.round(forecastItem.main.temp);
const forecastDayElement = document.createElement("div");
forecastDayElement.classList.add("forecast-day");
forecastDayElement.innerHTML = `
<p>${forecastDay}</p>
<img src="https://openweathermap.org/img/wn/${forecastItem.weather[0].icon}.png" >
<p>${forecastTemp}°C</p>
`;
forecastDaysDisplay.appendChild(forecastDayElement);
}
}
searchBox.addEventListener("keypress", async (event) => {
if (event.key === "Enter") {
const city = searchBox.value.trim();
if (city) {
const weatherData = await fetchWeatherData(city);
if (weatherData) {
displayWeatherData(weatherData);
const forecastData = await fetchForecastData(city);
if (forecastData) {
displayForecastData(forecastData);
}
} else {
displayWeatherData(null);
}
}
}
});