-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
99 lines (85 loc) · 3.28 KB
/
script.js
File metadata and controls
99 lines (85 loc) · 3.28 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
console.log("Javascript is connected!");
const searchbtn = document.getElementById('search-btn');
const cityInput = document.getElementById('city-input');
const themeToggle = document.getElementById('theme-toggle');
searchbtn.addEventListener('click', () => {
console.log("🔍 Search clicked");
const city = cityInput.value.trim();
if (city) {
fetchWeather(city);
} else {
alert("⚠️ No city entered.");
}
});
themeToggle.addEventListener('click', () => {
console.log("🌙 Toggle theme clicked");
document.body.classList.toggle('dark');
});
function fetchWeather(city) {
const apiKey = "efe47813118fbd5d0f7d5d78aa8fe45f";
const currentUrl = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}&units=metric`;
const forecastUrl = `https://api.openweathermap.org/data/2.5/forecast?q=${city}&appid=${apiKey}&units=metric`;
fetch(currentUrl)
.then(response => response.json())
.then(data => {
document.getElementById("city-name").textContent = data.name;
document.getElementById("temperature").textContent = `${data.main.temp}°C`;
document.getElementById("condition").textContent = data.weather[0].description;
document.getElementById("humidity").textContent = `${data.main.humidity}%`;
document.getElementById("wind").textContent = `${data.wind.speed} km/h`;
updateRecentSearches(city);
})
.catch(error => {
console.error("❌ Error fetching weather:", error);
alert("⚠️ Could not fetch weather data. Please try again.");
});
fetch(forecastUrl)
.then(response => response.json())
.then(data => {
const forecastSection = document.querySelector("#forecast");
forecastSection.innerHTML = "";
const dailyForecasts = {};
for (let item of data.list) {
const [date, time] = item.dt_txt.split(" ");
if (time === "12:00:00" && !dailyForecasts[date]) {
dailyForecasts[date] = item;
}
}
Object.values(dailyForecasts).forEach(day => {
const div = document.createElement("div");
div.className = "forecast-day";
div.innerHTML = `
<strong>${new Date(day.dt_txt).toDateString()}</strong>
<div>${day.main.temp}°C</div>
<div>${day.weather[0].description}</div>
`;
forecastSection.appendChild(div);
});
});
}
function updateRecentSearches(city) {
let searches = JSON.parse(localStorage.getItem("recentSearches")) || [];
searches = searches.filter(item => item.toLowerCase() !== city.toLowerCase());
searches.unshift(city);
if (searches.length > 5) {
searches.pop();
}
localStorage.setItem("recentSearches", JSON.stringify(searches));
renderRecentSearches();
}
function renderRecentSearches() {
const container = document.querySelector("#recent-searches");
container.innerHTML = "";
const searches = JSON.parse(localStorage.getItem("recentSearches")) || [];
searches.forEach(city => {
const btn = document.createElement("button");
btn.textContent = city;
btn.className = "recent-btn";
btn.addEventListener("click", () => fetchWeather(city));
container.appendChild(btn);
});
}
document.getElementById("clear-recent-btn").addEventListener("click", () => {
localStorage.removeItem("recentSearches");
renderRecentSearches();
});