-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
101 lines (75 loc) · 2.97 KB
/
Copy pathscript.js
File metadata and controls
101 lines (75 loc) · 2.97 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
document.addEventListener("DOMContentLoaded", function () {
const cityInput = document.getElementById("city-input");
const getWeatherBtn = document.getElementById("getWeatherbutton");
const weatherInfo = document.getElementById("weather-info");
const cityName = document.getElementById("cityName");
const temperatureDisplay = document.getElementById("temperature");
const descriptionDisplay = document.getElementById("description");
const errorMessage = document.getElementById("error-message");
const forecastContainer = document.getElementById("forecast");
const API_KEY = "YOUR_API_KEY";
getWeatherBtn.addEventListener("click", async () => {
const city = cityInput.value.trim();
if (city === "") {
showError("⚠️ Please enter a city name");
return;
}
try {
const currentWeather = await fetchWeatherData(city);
const forecastData = await fetchForecastData(city);
displayWeatherData(currentWeather);
displayForecastData(forecastData);
} catch (error) {
showError("❌ City not found. Please try again.");
}
});
async function fetchWeatherData(city) {
const url = `https://api.openweathermap.org/data/2.5/weather?q=${city}&units=metric&appid=${API_KEY}`;
const response = await fetch(url);
if (!response.ok) {
throw new Error("Weather fetch failed");
}
return await response.json();
}
async function fetchForecastData(city) {
const url = `https://api.openweathermap.org/data/2.5/forecast?q=${city}&units=metric&appid=${API_KEY}`;
const response = await fetch(url);
if (!response.ok) {
throw new Error("Forecast fetch failed");
}
return await response.json();
}
function displayWeatherData(data) {
const { name, main, weather } = data;
cityName.textContent = name;
temperatureDisplay.textContent = `🌡 Temperature: ${main.temp} °C`;
descriptionDisplay.textContent = `🌥 Condition: ${weather[0].description}`;
weatherInfo.classList.remove("hidden");
errorMessage.classList.add("hidden");
}
function displayForecastData(data) {
forecastContainer.innerHTML = "";
const dailyForecasts = data.list.filter(f => f.dt_txt.includes("12:00:00"));
dailyForecasts.forEach(day => {
const date = new Date(day.dt_txt).toDateString();
const temp = day.main.temp;
const desc = day.weather[0].description;
const icon = day.weather[0].icon;
const card = document.createElement("div");
card.classList.add("forecast-card");
card.innerHTML = `
<h4>${date}</h4>
<img src="https://openweathermap.org/img/wn/${icon}@2x.png" alt="${desc}">
<p>🌡 ${temp} °C</p>
<p>${desc}</p>
`;
forecastContainer.appendChild(card);
});
}
function showError(msg = "Something went wrong") {
weatherInfo.classList.add("hidden");
errorMessage.textContent = msg;
errorMessage.classList.remove("hidden");
forecastContainer.innerHTML = "";
}
});