-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
84 lines (76 loc) · 3.27 KB
/
Copy pathscript.js
File metadata and controls
84 lines (76 loc) · 3.27 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
const apiKey = '1ec7eb2fee28eab8f415643630a7c653'; // Replace this with your OpenWeatherMap API key
const weatherIcon = document.getElementById('weatherIcon');
const locationDisplay = document.getElementById('location');
const temperatureDisplay = document.getElementById('temperature');
const descriptionDisplay = document.getElementById('description');
const windSpeedDisplay = document.getElementById('windSpeed');
const humidityDisplay = document.getElementById('humidity');
const searchButton = document.getElementById('searchButton');
const locationInput = document.getElementById('locationInput');
// Event listener for search button
searchButton.addEventListener('click', () => {
const city = locationInput.value;
if (city) {
fetchWeatherData(city);
} else {
alert("Please enter a city name.");
}
});
// Function to fetch weather data
function fetchWeatherData(city) {
const apiUrl = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}&units=metric`; // Corrected string interpolation
fetch(apiUrl)
.then(response => {
if (!response.ok) {
throw new Error("City not found");
}
return response.json();
})
.then(data => {
updateWeatherInfo(data);
})
.catch(error => {
alert(error.message);
});
}
// Function to update weather information on the page
function updateWeatherInfo(data) {
const location = `${data.name}, ${data.sys.country}`;
const temperature = `${Math.round(data.main.temp)}°C`;
const description = data.weather[0].description;
const windSpeed = `Wind Speed: ${data.wind.speed} m/s`;
const humidity = `Humidity: ${data.main.humidity}%`;
// Update DOM elements
locationDisplay.textContent = location;
temperatureDisplay.textContent = temperature;
descriptionDisplay.textContent = description;
windSpeedDisplay.textContent = windSpeed;
humidityDisplay.textContent = humidity;
// Change weather icon based on weather conditions
const weatherCondition = data.weather[0].main.toLowerCase();
updateWeatherIcon(weatherCondition);
}
// Function to change weather icon dynamically
function updateWeatherIcon(condition) {
// Convert the condition string to lowercase to make the comparison case-insensitive
const lowerCaseCondition = condition.toLowerCase();
// Ensure weatherIcon is defined and exists
if (typeof weatherIcon !== 'undefined' && weatherIcon) {
if (lowerCaseCondition.includes('clear')) {
weatherIcon.src = 'sun.svg';
} else if (lowerCaseCondition.includes('clouds')) {
weatherIcon.src = 'cloud.svg';
} else if (lowerCaseCondition.includes('rain')) {
weatherIcon.src = 'rain.svg';
} else if (lowerCaseCondition.includes('snow')) {
weatherIcon.src = 'snow.svg';
} else {
weatherIcon.src = 'weather.svg'; // Default icon
}
// Smooth icon transition
weatherIcon.classList.add('fade-in');
setTimeout(() => weatherIcon.classList.remove('fade-in'), 500);
} else {
console.error('weatherIcon is not defined or does not exist.');
}
}