Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weather App</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
<link href="style.css" rel="stylesheet">
</head>
<body>
<div class="container mt-5 center-content">
<h1 class="mb-4 text-center">Weather App</h1>
<div class="form-group text-center">
<input type="text" id="cityInput" class="form-control" placeholder="Enter city name">
<button id="searchBtn" class="btn btn-primary mt-2">Search</button>
</div>
<div id="weatherInfo" class="mt-4"></div>
<div id="forecastInfo" class="mt-4"></div>
</div>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<script src="main.js"></script>
</body>
</html>
54 changes: 54 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
$(document).ready(function () {
const apiKey = '9a8359cd2576df91c6c91e74bb622c7a';
const baseUrl = 'http://api.openweathermap.org/data/2.5';
const weatherInfo = $('#weatherInfo');
const forecastInfo = $('#forecastInfo');
let city = '';

$('#searchBtn').click(function () {
city = $('#cityInput').val().trim();
if (city === '') {
alert('Please enter a city name');
return;
}

fetchCurrentWeather();
fetchForecast();
});

function fetchCurrentWeather() {
const currentWeatherUrl = `${baseUrl}/weather?q=${city}&appid=${apiKey}&units=imperial`;
fetch(currentWeatherUrl)
.then(response => response.json())
.then(data => {
const { name, main, weather } = data;
const currentWeatherHtml = `
<h2>Current Weather in ${name}</h2>
<p>Temperature: ${main.temp}°F</p>
<p>Conditions: ${weather[0].description}</p>
<img src="http://openweathermap.org/img/wn/${weather[0].icon}.png">
`;
weatherInfo.html(currentWeatherHtml);
Comment on lines +24 to +31

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

separating responsibilities and creating functions can help with readability.
This is not really fetching the weather, this is already rendering something... (hint for function name)

})

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing error call back. What happens if it fails?

}

function fetchForecast() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check your code indentation. Its confusing and the scope is getting lost

const forecastUrl = `${baseUrl}/forecast?q=${city}&appid=${apiKey}&units=imperial`;
fetch(forecastUrl)
.then(response => response.json())
.then(data => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also missing error handling

const forecast = data.list.filter(item => item.dt_txt.includes('12:00:00'));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is fine, but ideally you could have averaged the data according to the current time you are.

const forecastHtml = forecast.map(item => `
<div class="card mb-2">
<div class="card-body">
<h5 class="card-title">${new Date(item.dt * 1000).toLocaleDateString('en-US', { weekday: 'long' })}</h5>
<p>Temperature: ${item.main.temp}°F</p>
<p>Conditions: ${item.weather[0].description}</p>
<img src="http://openweathermap.org/img/wn/${item.weather[0].icon}.png">
</div>
</div>
`).join('');
forecastInfo.html(`<h2>5-Day Forecast</h2>${forecastHtml}`);
})
}
});
14 changes: 14 additions & 0 deletions style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
body {
background-color: #f8f9fa;
}

.center-content {
display: flex;
flex-direction: column;
align-items: center;
}

#forecastInfo {
display: flex;
flex-direction: row;
}