Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
47 lines (40 loc) · 1.58 KB
/
Copy pathscript.js
File metadata and controls
47 lines (40 loc) · 1.58 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
function getWeather() {
const apiKey = 'f823be7a367d09b63ec4322a0ef7d7b8';
const city = document.getElementById('city-input').value;
const url = `https://api.openweathermap.org/data/2.5/weather?q=${city}&units=metric&appid=${apiKey}`;
fetch(url)
.then(response => response.json())
.then(data => updateWeatherUI(data))
.catch(error => alert('City not found! Please try again.'));
}
function updateWeatherUI(data) {
if (data.cod !== 200) {
alert('City not found! Please try again.');
return;
}
const emojiMap = {
Clear: '☀️',
Clouds: '☁️',
Rain: '🌧️',
Drizzle: '🌦️',
Thunderstorm: '⛈️',
Snow: '❄️',
Mist: '🌫️',
Smoke: '💨',
Haze: '🌁',
Dust: '🌪️',
Fog: '🌫️',
Sand: '🏜️',
Ash: '🌋',
Squall: '🌬️',
Tornado: '🌪️'
};
const weatherEmoji = emojiMap[data.weather[0].main] || '🌍';
document.getElementById('city-name').textContent = data.name;
document.getElementById('weather-emoji').textContent = weatherEmoji;
document.getElementById('temperature').textContent = `${Math.round(data.main.temp)}°C`;
document.getElementById('weather-description').textContent = data.weather[0].description;
document.getElementById('humidity').textContent = `Humidity: ${data.main.humidity}%`;
document.getElementById('wind-speed').textContent = `Wind Speed: ${data.wind.speed} m/s`;
document.getElementById('weather-result').style.display = 'block';
}