-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
46 lines (39 loc) · 1.6 KB
/
script.js
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
// inputs
const inputBox = document.querySelector(".input-box");
const searchBtn = document.getElementById("searchBtn");
const weather_img = document.querySelector(".weather-img");
const temperature = document.querySelector(".temperature");
const description = document.querySelector(".description");
const humidity = document.getElementById("humidity");
const wind_speed = document.getElementById("windspeed");
const location_not_found = document.querySelector(".location-not-found");
const weather_body = document.querySelector(".weather-body");
//click action on search & display location
//function
async function checkWeather(city) {
const api_key = "214cb09cdb27fb9cbd176137a6310d14";
const url = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${api_key}`;
//fetching
const weather_data = await fetch(`${url}`).then((response) =>
response.json()
);
//output in console
console.log(weather_data);
if (weather_data.cod === "404") {
location_not_found.style.display = "flex";
weather_body.style.display = "none";
console.log("error");
return;
}
location_not_found.style.display = "none";
weather_body.style.display = "flex";
//Accessing data from API
temperature.innerHTML = `${Math.round(weather_data.main.temp - 273.15)}°C`;
description.innerHTML = `${weather_data.weather[0].description}`;
humidity.innerHTML = `${weather_data.main.humidity}%`;
wind_speed.innerHTML = `${weather_data.wind.speed}Km/H`;
}
//function call
searchBtn.addEventListener("click", () => {
checkWeather(inputBox.value);
});