forked from geekguyadarsh/WeatherAPP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
41 lines (34 loc) · 1.26 KB
/
script.js
File metadata and controls
41 lines (34 loc) · 1.26 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
// API_KEY for maps api
let API_KEY = "a8e71c9932b20c4ceb0aed183e6a83bb";
// Retrieve weather data from openweathermap
getWeatherData = (city) => {
const URL = "https://api.openweathermap.org/data/2.5/weather";
//HINT: Use template literals to create a url with input and an API key
const fullURL = `${URL}?q=${city}&appid=${API_KEY}&units=metric`;
weatherPromise = fetch(fullURL);
return weatherPromise
.then((response) => {
return response.json();
})
.catch((err) => console.log(err));
};
// Retrieve city input and get the weather data
searchCity = () => {
const city = document.getElementById("city-input").value;
data = getWeatherData(city);
return data
.then((response) => {
console.log(response);
showWeatherData(response);
})
.catch((err) => console.log(err));
};
// Show the weather data in HTML
showWeatherData = (weatherData) => {
document.getElementById("city-name").innerHTML = weatherData.name;
document.getElementById("weather-type").innerText =
weatherData.weather[0].main;
document.getElementById("temp").innerText = weatherData.main.temp;
document.getElementById("min-temp").innerText = weatherData.main.temp_min;
document.getElementById("max-temp").innerText = weatherData.main.temp_max;
};