-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
51 lines (42 loc) · 1.55 KB
/
script.js
File metadata and controls
51 lines (42 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"use strict";
let formEl = document.querySelector("#search");
let cityInputEl = document.querySelector("#city");
let tempEl = document.querySelector("#temp");
let messageEl = document.querySelector("#message");
async function getData() {
// Fetch data from Open Weather Map API, passing the input value as city
let res = await fetch(
`https://api.openweathermap.org/data/2.5/weather?q=${cityInputEl.value}&appid=8f20807cea52eed92572aea82df038d5`
);
if (!res.ok) {
alert(
`ERROR. ${cityInputEl.value} is invalid value. Input an existing city`
);
return;
}
let data = await res.json();
// We get temperatures back in Kelvin so we need to convert nto Celsius
// https://www.rapidtables.com/convert/temperature/kelvin-to-celsius.html
let temp = Math.round(data.main.temp - 273.15);
tempEl.textContent = `${temp}°C`;
// Different temperature ranges should print different messages:
//
// Below 0 = Winter is coming
// 0-10 = Sweater weather
// 11-20 = Put a jacket on and regret it as soon as you start moving
// Above 21 = Hotter outside than Taylor Swift's latest single
if (temp < 0) {
messageEl.textContent = "Winter is coming...";
} else if (temp <= 10) {
messageEl.textContent = "Sweater weather!";
} else if (temp <= 20) {
messageEl.textContent =
"Put a jacket on and regret it as soon as you start moving";
} else {
messageEl.textContent = "Hotter outside than Taylor Swift's latest single";
}
}
formEl.addEventListener("submit", function (e) {
e.preventDefault();
getData();
});