-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
94 lines (79 loc) · 2.98 KB
/
script.js
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
function getWeather() {
const apiKey = fb9c35ae2dc4e927d992e557168c9dd6;
const city = document.getElementById("city").value;
if (!city) {
alert("Please enter a city");
return;
}
const currentWeatherUrl = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}`;
const forecastUrl = `https://api.openweathermap.org/data/2.5/forecast?q=${city}&appid=${apiKey}`;
fetch(currentWeatherUrl)
.then((response) => response.json())
.then((data) => {
displayWeather(data);
})
.catch((error) => {
console.error("Cannot find current weather data:", error);
alert("Cannot find current weather data. Please try again.");
});
fetch(forecastUrl)
.then((response) => response.json())
.then((data) => {
displayHourlyForecast(data.list);
})
.catch((error) => {
console.error("Cannot find hourly forecast data:", error);
alert("Cannot find hourly forecast data. Please try again");
});
}
function displayWeather(data) {
const tempDivInfo = document.getElementById("temp-div");
const weatherInfoDiv = document.getElementById("weather-info");
const weatherIcon = document.getElementById("weather-icon");
const hourlyForecastDiv = document.getElementById("hourly-forecast");
//Clear previous content
weatherInfoDiv.innerHTML = "";
hourlyForecastDiv.innerHTML = "";
tempDivInfo.innerHTML = "";
if (data.cod == "404") {
weatherInfoDiv.innerHTML = `<p>${data.message}</p>`;
} else {
const cityName = data.name;
const temperature = Math.round(((data.main.temp - 273.15) * 9) / 5 + 32);
const iconCode = data.weather[0].icon;
const iconUrl = `https://openweathermap.org/img/wn/${iconCode}@4x.png`;
const temperatureHTML = `<p>${temperature}°F</p>`;
const weatherHTML = `
<p>${cityName}</p>
<p>${description}</p>
`;
tempDivInfo.innerHTML = temperatureHTML;
weatherInfoDiv.innerHTML = weatherHTML;
weatherIcon.src = iconUrl;
weatherIcon.alt = description;
showImage();
}
}
function displayHourlyForecast(hourlyData) {
const hourlyForecastDiv = document.getElementById("hourly-forecast");
const next24Hrs = hourlyData.slice(0, 8);
next24Hrs.forEach((item) => {
const dateTime = new Date(item.dt * 1000);
const hour = dateTime.getHours();
const temperature = Math.round(((item.main.temp - 273.15) * 9) / 5 + 32);
const iconCode = item.weather[0].icon;
const iconUrl = `https://openweathermap.org/img/wn/${iconCode}.png`;
const hourlyItemHtml = `
<div class"hourly-item">
<span>${hour}:00</span>
<img src="${iconUrl}" alt="Hourly Weather Icon">
<span>${temperature}</span>
</div>
`;
hourlyForecastDiv.innerHTML += hourlyItemHtml;
});
}
function showImage() {
const weatherIcon = document.getElementById("weather-icon");
weatherIcon.style.display = "block";
}