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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
config.js
44 changes: 44 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css"
integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N"
crossorigin="anonymous"
/>
<link rel="stylesheet" href="styles.css" />
<title>Weather Project</title>
</head>
<body>
<div class="container text-center">
<h1 class="text-center pb-2 mb-4 border-bottom">Weather Project</h1>

<form>
<div class="row justify-content-center">
<div class="col-sm-4 my-1">
<label class="sr-only" for="inlineFormInputName">Name</label>
<input
type="text"
class="form-control"
id="inlineFormInputName"
placeholder="City"
/>
</div>
<div class="col-auto my-1">
<button type="submit" class="btn btn-primary" id="searchBtn">
Submit
</button>
</div>
</div>
</form>
</div>
<div class="container text-center">
<div id="currentWeather"></div>
</div>
<div class="card-group"></div>
<script type="module" src="main.js"></script>

Choose a reason for hiding this comment

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

getting cors using this approach. I had to remove the API key import. This wasn't working as you didn't commit your key (good). But you need to submit working code.

</body>
</html>
125 changes: 125 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import { API_KEY } from "./config.js";

let currentWeatherData = [];
let fiveDayForecastData = [];

let renderCurrentWeather = () => {
document.querySelector('#currentWeather').replaceChildren();

for (let i = 0; i < currentWeatherData.length; i++) {
const template = `
<div class="weatherProfile">
<h2 class="temp">${Math.ceil(currentWeatherData[i].temperature)}°</h2>
<h2 class="city">${currentWeatherData[i].cityName}</h2>
<h2 class="condition">${currentWeatherData[i].weatherCondition}</h2>
</div>
<div class="weatherIcon">
<img src=${currentWeatherData[i].weatherIcon}>
</div>
`
document.querySelector("#currentWeather").insertAdjacentHTML("beforeend", template);
}
}

let renderFiveDayWeather = () => {
document.querySelector('.card-group').replaceChildren();

for( let i = 0; i < fiveDayForecastData.length; i++) {
let dayData = fiveDayForecastData[i];
const template = `
<div class="card border-dark">
<div class="card-body">
<p class="card-text text-center">${dayData.condition}</p>
<p class="card-text text-center">${dayData.temp}°</p>
<img src=${dayData.icon} class="card-img-top" alt="weather icon">
<p class="card-text text-center">${dayData.day}</p>
</div>
</div>
`
document.querySelector(".card-group").insertAdjacentHTML("beforeend", template);
}
}

document.querySelector("#searchBtn").addEventListener("click", (e) => {
e.preventDefault();

let search = document.querySelector("#inlineFormInputName").value;

fetchCurrentWeather(search);
fetchfiveDayWeather(search);

document.querySelector("#inlineFormInputName").value = "";
});

let fetchCurrentWeather = (city) => {
const weatherUrl = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${API_KEY}&units=imperial`;

fetch(weatherUrl , {
method: "GET",
dataType: "json"
})
.then(res => res.json())
.then(res => {
addCurrentWeather(res);
})
}

let fetchfiveDayWeather = (city) => {
const fiveDayWeatherUrl = `https://api.openweathermap.org/data/2.5/forecast?q=${city}&appid=${API_KEY}&units=imperial`;

fetch(fiveDayWeatherUrl, {
method: "GET",
dataType: "json"
})
.then(res => res.json())
.then(res => {

addFiveDayWeather(res);
})
}

let addFiveDayWeather = (data) => {
fiveDayForecastData = [];

for (let i = 0; i < data.list.length; i += 8) {
const days = data.list.slice(i, i + 8);

const temps = days.map(value => value.main.temp);
const avgTemp = temps.reduce((sum, t) => sum + t, 0)/temps.length;
const roundedTemp = Math.ceil(avgTemp);

const date = new Date(days[0].dt_txt);
const dayNames = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
const dayOfWeek = dayNames[date.getDay()];

const icon = `https://openweathermap.org/img/wn/${days[0].weather[0].icon}@2x.png`;

const condition = days[0].weather[0].main;

fiveDayForecastData.push({
condition: condition,
temp: roundedTemp,
icon: icon,
day: dayOfWeek,
});
}

renderFiveDayWeather(fiveDayForecastData);
}

let addCurrentWeather = (res) => {
currentWeatherData = [];

const weatherProfile = {
temperature: res.main.temp,
cityName: res.name,
weatherCondition: res.weather[0].main,
weatherIcon: `https://openweathermap.org/img/wn/${res.weather[0].icon}@2x.png`
}

currentWeatherData.push(weatherProfile);

renderCurrentWeather(currentWeatherData);
}


35 changes: 35 additions & 0 deletions styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
h1 {
font-weight: bold;
font-size: 60px;
}

.row {
margin-top: 60px;
}

#currentWeather {
display:flex;
justify-content: center;
align-items: center;
gap: 300px;
margin-top: 40px;
}

.city {
font-size: 30px;
font-weight: bold;
}

.card {
max-width: 250px;
}

.card-group {
justify-content: center;
margin-top: 40px;
}

.card-text {
font-weight: bold;
}