-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
58 lines (48 loc) · 1.89 KB
/
app.js
File metadata and controls
58 lines (48 loc) · 1.89 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
const cityName = document.querySelector("#city");
const btn = document.querySelector("#searchBtn");
const box = document.querySelector("#box");
const boxMsg = document.querySelector("#boxMsg");
const weatherData = document.querySelector("#weatherData");
const disCity = document.querySelector("#disCity");
const disTemp = document.querySelector("#disTemp");
const disHum = document.querySelector("#disHum");
const disIcon = document.querySelector("#disIcon");
const disLine = document.querySelector("#disLine");
const baseUrl = "https://api.openweathermap.org/data/2.5/weather";
const apiKey = "54ba27020a73b37672c2a775d6b2a628";
window.onload = () => {
cityName.value = "";
boxMsg.innerText = "Please enter a city name!";
boxMsg.style.display = "flex";
weatherData.style.display = "none";
};
btn.onclick = async () => {
const city = cityName.value;
if (city === "") {
boxMsg.innerText = "Please enter a city name!";
boxMsg.style.display = "flex";
weatherData.style.display = "none";
return;
}
else{
const url = `${baseUrl}?q=${city}&units=metric&appid=${apiKey}`;
let promise = await fetch(url);
let data = await promise.json();
console.log(data);
if (data.cod !== 200) {
boxMsg.innerText = "City not found!";
boxMsg.style.color = "red";
boxMsg.style.display = "flex";
weatherData.style.display = "none";
return;
}
boxMsg.style.display = "none";
weatherData.style.display = "flex";
disCity.innerText = data.name;
disTemp.innerHTML = `${Math.round(data.main.temp)}<sup>°C</sup>`;
disHum.innerText = `${data.wind.speed} km/h`;
disLine.innerText = data.weather[0].description;
const iconCode = data.weather[0].icon;
disIcon.src = `https://openweathermap.org/img/wn/${iconCode}@2x.png`;
}
}