-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
95 lines (78 loc) · 3.29 KB
/
script.js
File metadata and controls
95 lines (78 loc) · 3.29 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
async function getWeather() {
console.log('getWeather function called');
const apiKey = "36948041c736470069f30c95042c8cc6";
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}&units=metric&APPID=${apiKey}`;
const forecastUrl = `https://api.openweathermap.org/data/2.5/forecast?q=${city}&units=metric&APPID=${apiKey}`;
try {
console.log(`Fetching current weather from: ${currentWeatherUrl}`);
const currentWeatherResponse = await fetch(currentWeatherUrl);
const currentWeatherData = await currentWeatherResponse.json();
console.log('Current weather response:', currentWeatherData);
if (currentWeatherData.cod !== 200) {
alert(currentWeatherData.message);
return;
}
displayWeather(currentWeatherData);
console.log(`Fetching forecast from: ${forecastUrl}`);
const forecastResponse = await fetch(forecastUrl);
const forecastData = await forecastResponse.json();
console.log('Forecast response:', forecastData);
if (forecastData.cod !== "200") {
alert(forecastData.message);
return;
}
displayHourlyForecast(forecastData.list);
} catch (error) {
console.error('Error fetching weather data:', error);
alert('Error fetching weather data. Please try again.');
}
}
function displayWeather(data) {
const tempDivInfo = document.getElementById('tem-div');
const weatherInfoDiv = document.getElementById('weather-info');
const weatherIcon = document.getElementById('weather-icon');
const hourlyForecastDiv = document.getElementById('hourly-forecast');
weatherInfoDiv.innerHTML = '';
hourlyForecastDiv.innerHTML = '';
tempDivInfo.innerHTML = '';
const cityName = data.name;
const temperature = Math.round(data.main.temp);
const description = data.weather[0].description;
const iconCode = data.weather[0].icon;
const iconUrl = `https://openweathermap.org/img/wn/${iconCode}@4x.png`;
const temperatureHTML = `<p>${temperature}°C</p>`;
const weatherHtml = `
<p>${cityName}</p>
<p>${description}</p>
`;
tempDivInfo.innerHTML = temperatureHTML;
weatherInfoDiv.innerHTML = weatherHtml;
weatherIcon.src = iconUrl;
weatherIcon.alt = description;
weatherIcon.style.display = 'block';
}
function displayHourlyForecast(hourlyData) {
const hourlyForecastDiv = document.getElementById('hourly-forecast');
const next24Hours = hourlyData.slice(0, 8);
let hourlyForecastHTML = '';
next24Hours.forEach(item => {
const dateTime = new Date(item.dt * 1000);
const hour = dateTime.getHours();
const temperature = Math.round(item.main.temp);
const iconCode = item.weather[0].icon;
const iconUrl = `https://openweathermap.org/img/wn/${iconCode}.png`;
hourlyForecastHTML += `
<div class="hourly-item">
<span>${hour}:00</span>
<img src="${iconUrl}" alt="Hourly Weather Icon">
<span>${temperature}°C</span>
</div>
`;
});
hourlyForecastDiv.innerHTML = hourlyForecastHTML;
}