Skip to content

Commit

Permalink
Display current day, time and temperature of searched city
Browse files Browse the repository at this point in the history
  • Loading branch information
urgetolearn committed Oct 17, 2023
0 parents commit a547bf7
Show file tree
Hide file tree
Showing 3 changed files with 200 additions and 0 deletions.
86 changes: 86 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<link
rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T"
crossorigin="anonymous"
/>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<link rel="stylesheet" href="styles.css" />
<title>Weather App</title>
</head>

<body>
<div class="container">
<div class="weather-app">
<form class="search-form" id="search-form">
<div class="row">
<div class="col-9">
<input
type="search"
placeholder="Type a city.."
autofocus="on"
autocomplete="off"
id="city-input"
class="form-control shadow-sm"
/>
</div>
<div class="col-3">
<input
type="submit"
value="Search"
class="form-control btn btn-primary shadow-sm"
/>
</div>
</div>
</form>
<h1 id="city">
Sydney NSW, Australia
</h1>
<ul>
<li id="date"></li>
<li>
Partly Cloudy
</li>
</ul>
<div class="row">
<div class="col-6">
<div class="clearfix">
<img
src="https://ssl.gstatic.com/onebox/weather/64/partly_cloudy.png"
alt="Cloudy icon"
class="weather-icon float-left"
/>
<div class="float-left">
<span class="temperature" id="temperature">19</span
><span class="units">
<a href="#" id="celsius-link">°C</a> |
<a href="#" id="fahrenheit-link">°F</a>
</span>
</div>
</div>
</div>
<div class="col-6">
<ul>
<li>
Precipitation: 0%
</li>
<li>
Humidity: 77%
</li>
<li>
Wind: 8 km/h
</li>
</ul>
</div>
</div>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
59 changes: 59 additions & 0 deletions script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
function formatDate(date) {
let hours = date.getHours();
if (hours < 10) {
hours = `0${hours}`;
}
let minutes = date.getMinutes();
if (minutes < 10) {
minutes = `0${minutes}`;
}

let dayIndex = date.getDay();
let days = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
];
let day = days[dayIndex];

return `${day} ${hours}:${minutes}`;
}

function search(event) {
event.preventDefault();
let cityElement = document.querySelector("#city");
let cityInput = document.querySelector("#city-input");
cityElement.innerHTML = cityInput.value;
}

function convertToFahrenheit(event) {
event.preventDefault();
let temperatureElement = document.querySelector("#temperature");
temperatureElement.innerHTML = 66;
}

function convertToCelsius(event) {
event.preventDefault();
let temperatureElement = document.querySelector("#temperature");
temperatureElement.innerHTML = 19;
}

// Feature #1
let dateElement = document.querySelector("#date");
let currentTime = new Date();
dateElement.innerHTML = formatDate(currentTime);

// Feature #2
let searchForm = document.querySelector("#search-form");
searchForm.addEventListener("submit", search);

// Bonus Feature
let fahrenheitLink = document.querySelector("#fahrenheit-link");
fahrenheitLink.addEventListener("click", convertToFahrenheit);

let celsiusLink = document.querySelector("#celsius-link");
celsiusLink.addEventListener("click", convertToCelsius);
55 changes: 55 additions & 0 deletions styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
body {
font-family: arial, sans-serif;
}

h1 {
color: #7c7c7c;
font-size: 24px;
font-weight: 100;
line-height: 28px;
margin: 0;
}

ul {
margin: 0 0 10px;
padding: 0;
}

li {
color: #7c7c7c;
font-size: 16px;
font-weight: 100;
height: 19px;
line-height: 1;
list-style: none;
}

.weather-app {
border: 1px solid #dadde1;
padding: 15px;
margin: 20px auto;
border-radius: 10px;
max-width: 600px;
}

.weather-icon {
height: 64px;
width: 64px;
margin-right: 10px;
}

.temperature {
color: rgb(33, 33, 33);
font-size: 64px;
font-weight: 400;
line-height: 1;
}

.units {
position: relative;
top: -34px;
}

.search-form {
margin-bottom: 20px;
}

0 comments on commit a547bf7

Please sign in to comment.