Skip to content

Commit 94cf3cf

Browse files
authored
Add files via upload
1 parent e0ff600 commit 94cf3cf

File tree

3 files changed

+82
-10
lines changed

3 files changed

+82
-10
lines changed

index.html

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,22 @@
1818
<body>
1919
<div class="top">
2020
<h1>Weather App</h1>
21-
<form class="" action="index.html" method="post">
22-
<input type="text" name="search-by-city" value="" placeholder="Search by city name">
21+
<form action="" method="post" onsubmit="return false">
22+
<input type="text" class = "search" name="search-by-city" value="" placeholder="Search by city name">
2323
<br>
24-
<button type="submit" name="button" class="btn btn-warning btn-lg">SUBMIT</button>
24+
<button type="submit" name="button" class="btn btn-warning btn-lg button-submit">SUBMIT</button>
2525
</form>
26-
<h3 class="vis">invalid city name</h3>
27-
<h2>Lagos, NG</h2>
28-
<p>Thursday, 26 Nov 2020</p>
26+
<h3 class="error"></h3>
27+
<h2 class = "city">Lagos, NG</h2>
28+
<p class = "date">Thursday, 26 Nov 2020</p>
2929
<hr>
3030
<div class="info">
31-
<p>Temp: 26 &#8451;</p>
32-
<p>Weather: Clouds</p>
33-
<p>Temp Range: 26&#8451;/ 26&#8451;</p>
31+
<p class= "temp">Temp: 26 &#8451;</p>
32+
<p class = "weather">Weather: Clouds</p>
33+
<p class = "temp-range">Temp Range: 26&#8451;/ 26&#8451;</p>
3434
</div>
3535
</div>
36+
<script type="text/javascript" src="index.js">
37+
</script>
3638
</body>
3739
</html>

index.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
const api = {
2+
key:"28fd15358cdecbc1a1dfef367e71acef",
3+
base: "api.openweathermap.org/data/2.5/"
4+
}
5+
6+
const search = document.querySelector(".search");
7+
const btn = document.querySelector(".button-submit");
8+
btn.addEventListener("click", getInput);
9+
10+
function getInput (event) {
11+
event.preventDefault();
12+
if (event.type == "click") {
13+
getData(search.value);
14+
//console.log(search.value);
15+
//console.log(`${api.base}weather?q=${search.value}&units=metric&appid=${api.key}`);
16+
}
17+
}
18+
19+
function getData () {
20+
fetch(`https://${api.base}weather?q=${search.value}&units=metric&appid=${api.key}`)
21+
.then(response => {
22+
return response.json();
23+
}).then(displayData);
24+
25+
}
26+
27+
function displayData (response) {
28+
console.log(response);
29+
if (response.cod === "404") {
30+
const error = document.querySelector(".error");
31+
error.textContent = "Please enter a valid city";
32+
search.value = "";
33+
} else {
34+
const error = document.querySelector(".error");
35+
error.textContent = "";
36+
const city = document.querySelector(".city");
37+
city.innerText = `${response.name}, ${response.sys.country}`;
38+
39+
const today = new Date();
40+
const date = document.querySelector(".date");
41+
date.innerText = dateFunction(today);
42+
43+
const temp = document.querySelector(".temp");
44+
temp.innerHTML = `Temp: ${Math.round(response.main.temp)} <span>°C</span>`;
45+
46+
const weather = document.querySelector(".weather");
47+
weather.innerText = `Weather: ${response.weather[0].main}`;
48+
49+
const tempRange = document.querySelector(".temp-range");
50+
tempRange.innerText = `Temp Range: ${Math.round(response.main.temp_min)}°C / ${Math.round(response.main.temp_max)}°C`;
51+
52+
search.value = "";
53+
}
54+
}
55+
56+
function dateFunction (d) {
57+
let months = ["Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec"];
58+
let days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
59+
60+
let day = days[d.getDay()];
61+
let date = d.getDate();
62+
let month = months[d.getMonth()];
63+
let year = d.getFullYear();
64+
65+
return `${day}, ${date} ${month} ${year}`;
66+
}

styles.css

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,12 @@ p {
5656
color: white;
5757
border-radius: 3%;
5858
width: auto;
59-
max-width: 900px;
59+
max-width: 600px;
6060
overflow: hidden;
61+
display: flex;
62+
flex-direction: column;
63+
align-items: center;
64+
justify-content: center;
6165
}
6266

6367

0 commit comments

Comments
 (0)