forked from sumn2u/learn-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
79 lines (70 loc) · 2.3 KB
/
script.js
File metadata and controls
79 lines (70 loc) · 2.3 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const city = document.querySelector(".city-name-input");
const temp = document.querySelector(".temperature");
const feels_like = document.querySelector(".feels-like");
const humidity = document.querySelector(".humidity");
const windspeed = document.querySelector(".windspeed");
const btn = document.querySelector(".srch-btn");
const cityOp = document.querySelector(".city-name-output");
// IMPORTANT: Replace "YOUR_API_KEY" with your actual OpenWeatherMap API key.
const apiKey = "YOUR_API_KEY";
async function getWeatherInfo(city) {
try {
console.log(city);
if (city.length === 0) {
throw Error("Enter city name correctly");
}
let res = await fetch(
`https://api.openweathermap.org/data/2.5/weather?q=${city}&units=metric&appid=${apiKey}`
);
console.log(res);
if (!res.ok) {
if (res.status === 404) {
throw Error("City not found");
} else if (res.status === 401) {
throw Error("Access denied.Check your api key ");
}
}
res = await res.json();
console.log(res);
//wind speed , feels like, temp,humidity
const { feels_like, temp, humidity } = res.main;
const { speed } = res.wind;
const name = res.name;
const desc = res.weather[0].main;
assignWeatherDetails(speed, temp, humidity, feels_like, name, desc);
} catch (e) {
document.querySelector(".city-name-input").value = "";
console.log(e);
alert(e.message);
}
}
btn.addEventListener("click", () => getWeatherInfo(city.value.trim()));
city.addEventListener("keydown", (event) => {
if (event.key === "Enter") {
getWeatherInfo(city.value.trim());
}
});
function assignWeatherDetails(s, t, h, fl, n, desc) {
windspeed.innerHTML = s;
temp.innerHTML = t + "<sup>o</sup>C" + `<p>${desc}</p>`;
humidity.innerHTML = h;
feels_like.innerHTML = fl + "<sup>o</sup>C";
cityOp.innerHTML = n;
// Use textContent to prevent XSS vulnerabilities
windspeed.textContent = s;
humidity.textContent = h;
cityOp.textContent = n;
temp.innerHTML = "";
feels_like.innerHTML = "";
temp.textContent = t;
temp.insertAdjacentHTML("beforeend", "<sup>o</sup>C");
const descPara = document.createElement("p");
descPara.textContent = desc;
temp.appendChild(descPara);
feels_like.textContent = fl;
feels_like.insertAdjacentHTML("beforeend", "<sup>o</sup>C");
city.value = "";
}
window.addEventListener("load", () => {
getWeatherInfo("London");
});