-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
48 lines (41 loc) · 1.69 KB
/
app.js
File metadata and controls
48 lines (41 loc) · 1.69 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
const apiKey = "a38490a8eb8db21367a8231a4f357a73";
const url = "https://api.openweathermap.org/data/2.5/weather?units=metric&q=";
const searchBar = document.querySelector(".search input");
const searchBtn = document.querySelector(".search button");
const weather_status = document.querySelector(".weather-icon");
async function checkWeather(city) {
const response = await fetch(url + city + `&appid=${apiKey}`); // Use backticks for string interpolation
const data = await response.json();
console.log(data);
document.querySelector(".city").innerHTML = data.name;
document.querySelector(".temp").innerHTML = Math.round(data.main.temp) + "°C";
document.querySelector(".percentage_humidity").innerHTML =
data.main.humidity + "%";
document.querySelector(".wind_speed").innerHTML = data.wind.speed + " km/h";
if (data.weather[0].main == "Clear") {
weather_status.src = "images/sunny.gif";
} else if (data.weather[0].main == "Clouds") {
weather_status.src = "images/cloudy.gif";
} else if (data.weather[0].main == "Rain") {
weather_status.src = "images/rain.gif";
} else if (data.weather[0].main == "Drizzle") {
weather_status.src = "images/drizzle.gif";
} else if (data.weather[0].main == "Mist") {
weather_status.src = "images/mist.png";
} else if (data.weather[0].main == "Snow") {
weather_status.src = "images/snow.gif";
}
}
searchBar.addEventListener("keypress", function (event) {
if (event.key === "Enter") {
event.preventDefault();
document.querySelector(".search button").click();
}
});
searchBtn.addEventListener("click", () => {
if (searchBar.value == "") {
alert("Enter a city name!");
} else {
checkWeather(searchBar.value);
}
});