-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
55 lines (35 loc) · 1.24 KB
/
script.js
File metadata and controls
55 lines (35 loc) · 1.24 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
52
53
const apikey = "ef0f9306883aa2ee119e64f8b6a68353";
const main = document.getElementById("main");
const form = document.getElementById("form");
const search = document.getElementById("search");
const url = (city) =>
`https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apikey}`;
async function getWeatherByLocation(city) {
const resp = await fetch(url(city),{
origin: "cors"});
const respData = await resp.json();
console.log(respData);
addWeatherToPage(respData);
}
function addWeatherToPage(data) {
const temp = KToC(data.main.temp);
const weather = document.createElement("div");
weather.classList.add("weather");
weather.innerHTML = `
<div class="temp">${temp}<span>°C</span></div>
<div class="city">${search.value}, ${data.sys.country}</div>
<div class="wind">${data.weather[0].main}<img class="img" src="https://openweathermap.org/img/wn/${data.weather[0].icon}@2x.png" /></div>
`;
main.innerHTML = "";
main.appendChild(weather);
}
function KToC(K) {
return Math.floor(K - 273.15);
}
form.addEventListener("submit", (e) => {
e.preventDefault();
const city = search.value;
if (city) {
getWeatherByLocation(city);
}
});