-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
45 lines (39 loc) · 1.73 KB
/
script.js
File metadata and controls
45 lines (39 loc) · 1.73 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
const getWeather = async () => {
const city = document.getElementById('city').value;
const output = document.getElementById('output');
const error = document.getElementById('error');
output.innerHTML = '';
error.textContent = '';
if (!city) {
error.textContent = 'Please enter a city name.';
return;
}
try {
// Fetch coordinates of the city using Open-Meteo Geo API
const geoResponse = await fetch(`https://geocoding-api.open-meteo.com/v1/search?name=${city}`);
if (!geoResponse.ok) {
throw new Error('City not found');
}
const geoData = await geoResponse.json();
if (!geoData.results || geoData.results.length === 0) {
error.textContent = 'City not found. Please try a different city.';
return;
}
const { latitude, longitude } = geoData.results[0];
// Fetch weather data using coordinates
const weatherResponse = await fetch(`https://api.open-meteo.com/v1/forecast?latitude=${latitude}&longitude=${longitude}¤t_weather=true`);
if (!weatherResponse.ok) {
throw new Error('Failed to fetch weather data');
}
const weatherData = await weatherResponse.json();
const { temperature, weathercode } = weatherData.current_weather;
output.innerHTML = `
<p><strong>City:</strong> ${city}</p>
<p><strong>Temperature:</strong> ${temperature.toFixed(1)}°C</p>
<p><strong>Condition Code:</strong> ${weathercode}</p>
`;
} catch (err) {
error.textContent = 'Could not fetch weather data. Please try again later.';
}
};
document.getElementById('getWeather').addEventListener('click', getWeather);