├── README.md ├── index.html ├── script.js └── style.css /README.md: -------------------------------------------------------------------------------- 1 | # WeatherApplication 2 | A simple weather application using the openweather API. 3 | 4 | STEPS TO RUN THE CODE: 5 | 1. Go to https://openweathermap.org 6 | 2. Create a free account 7 | 3. Navigate to the API keys section 8 | 4. Generate a key 9 | 5. Copy that key to clipboard 10 | 6. Insert that key into the required variable in the script.js file 11 | 7. Enjoy! 12 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Weather App 7 | 8 | 9 | 10 |
11 |

Weather App

12 | 13 | 14 | 15 | Weather Icon 16 | 17 |
18 | 19 |
20 | 21 |
22 |
23 | 24 | 25 | -------------------------------------------------------------------------------- /script.js: -------------------------------------------------------------------------------- 1 | function getWeather() { 2 | const apiKey = 'YOUR-API-KEY'; 3 | const city = document.getElementById('city').value; 4 | 5 | if (!city) { 6 | alert('Please enter a city'); 7 | return; 8 | } 9 | 10 | const currentWeatherUrl = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}`; 11 | const forecastUrl = `https://api.openweathermap.org/data/2.5/forecast?q=${city}&appid=${apiKey}`; 12 | 13 | fetch(currentWeatherUrl) 14 | .then(response => response.json()) 15 | .then(data => { 16 | displayWeather(data); 17 | }) 18 | .catch(error => { 19 | console.error('Error fetching current weather data:', error); 20 | alert('Error fetching current weather data. Please try again.'); 21 | }); 22 | 23 | fetch(forecastUrl) 24 | .then(response => response.json()) 25 | .then(data => { 26 | displayHourlyForecast(data.list); 27 | }) 28 | .catch(error => { 29 | console.error('Error fetching hourly forecast data:', error); 30 | alert('Error fetching hourly forecast data. Please try again.'); 31 | }); 32 | } 33 | 34 | function displayWeather(data) { 35 | const tempDivInfo = document.getElementById('temp-div'); 36 | const weatherInfoDiv = document.getElementById('weather-info'); 37 | const weatherIcon = document.getElementById('weather-icon'); 38 | const hourlyForecastDiv = document.getElementById('hourly-forecast'); 39 | 40 | // Clear previous content 41 | weatherInfoDiv.innerHTML = ''; 42 | hourlyForecastDiv.innerHTML = ''; 43 | tempDivInfo.innerHTML = ''; 44 | 45 | if (data.cod === '404') { 46 | weatherInfoDiv.innerHTML = `

${data.message}

`; 47 | } else { 48 | const cityName = data.name; 49 | const temperature = Math.round(data.main.temp - 273.15); // Convert to Celsius 50 | const description = data.weather[0].description; 51 | const iconCode = data.weather[0].icon; 52 | const iconUrl = `https://openweathermap.org/img/wn/${iconCode}@4x.png`; 53 | 54 | const temperatureHTML = ` 55 |

${temperature}°C

56 | `; 57 | 58 | const weatherHtml = ` 59 |

${cityName}

60 |

${description}

61 | `; 62 | 63 | tempDivInfo.innerHTML = temperatureHTML; 64 | weatherInfoDiv.innerHTML = weatherHtml; 65 | weatherIcon.src = iconUrl; 66 | weatherIcon.alt = description; 67 | 68 | showImage(); 69 | } 70 | } 71 | 72 | function displayHourlyForecast(hourlyData) { 73 | const hourlyForecastDiv = document.getElementById('hourly-forecast'); 74 | 75 | const next24Hours = hourlyData.slice(0, 8); // Display the next 24 hours (3-hour intervals) 76 | 77 | next24Hours.forEach(item => { 78 | const dateTime = new Date(item.dt * 1000); // Convert timestamp to milliseconds 79 | const hour = dateTime.getHours(); 80 | const temperature = Math.round(item.main.temp - 273.15); // Convert to Celsius 81 | const iconCode = item.weather[0].icon; 82 | const iconUrl = `https://openweathermap.org/img/wn/${iconCode}.png`; 83 | 84 | const hourlyItemHtml = ` 85 |
86 | ${hour}:00 87 | Hourly Weather Icon 88 | ${temperature}°C 89 |
90 | `; 91 | 92 | hourlyForecastDiv.innerHTML += hourlyItemHtml; 93 | }); 94 | } 95 | 96 | function showImage() { 97 | const weatherIcon = document.getElementById('weather-icon'); 98 | weatherIcon.style.display = 'block'; // Make the image visible once it's loaded 99 | } -------------------------------------------------------------------------------- /style.css: -------------------------------------------------------------------------------- 1 | body { 2 | background: #8C52FF; 3 | font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; 4 | display: flex; 5 | align-items: center; 6 | justify-content: center; 7 | height: 100vh; 8 | margin: 0; 9 | } 10 | 11 | #weather-container { 12 | background: rgba(255, 255, 255, 0.3); 13 | max-width: 400px; 14 | padding: 20px; 15 | border-radius: 15px; 16 | box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1); 17 | backdrop-filter: blur(10px); 18 | border: 1px solid rgba(255, 255, 255, 0.1); 19 | text-align: center; 20 | } 21 | 22 | h2, label, p { 23 | color: #fff; 24 | margin: 8px 0; 25 | } 26 | 27 | input { 28 | width: calc(100% - 16px); 29 | padding: 8px; 30 | box-sizing: border-box; 31 | border-radius: 10px; 32 | border: 1px solid white; 33 | margin-top: 20px; 34 | } 35 | 36 | button { 37 | background: #debff4; 38 | color: white; 39 | padding: 10px; 40 | border: none; 41 | border-radius: 10px; 42 | cursor: pointer; 43 | margin-top: 20px; 44 | width: 100px; 45 | font-size: 15px; 46 | } 47 | 48 | button:hover { 49 | background: #8b48d7; 50 | } 51 | 52 | #temp-div p { 53 | font-size: 60px; 54 | margin-top: -30px; 55 | } 56 | 57 | #weather-info { 58 | font-size: 20px; 59 | } 60 | 61 | #weather-icon { 62 | width: 200px; 63 | height: 200px; 64 | margin: 0 auto 10px; 65 | margin-bottom: 0; 66 | display: none; 67 | } 68 | 69 | #hourly-forecast { 70 | margin-top: 50px; 71 | overflow-x: auto; 72 | white-space: nowrap; 73 | display: flex; 74 | justify-content: space-between; 75 | } 76 | 77 | .hourly-item { 78 | flex: 0 0 auto; 79 | width: 80px; 80 | display: flex; 81 | flex-direction: column; 82 | align-items: center; 83 | margin-right: 10px; 84 | color: white; 85 | } 86 | 87 | .hourly-item img { 88 | width: 30px; 89 | height: 30px; 90 | margin-bottom: 5px; 91 | } 92 | 93 | #hourly-heading { 94 | color: #fff; 95 | margin-top: 10px; 96 | } --------------------------------------------------------------------------------