-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
66 lines (51 loc) · 1.96 KB
/
Copy pathscript.js
File metadata and controls
66 lines (51 loc) · 1.96 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
const searchBar = document.getElementById("search-city");
const searchBtn = document.getElementById("search-btn");
const cityName = document.getElementById("city-name");
const cityTemp = document.getElementById("temperature");
const cityWind = document.getElementById("wind");
const humidity = document.getElementById("humidity");
const uvIndex = document.getElementById("uv");
const weatherConditions = document.getElementById("weather-conditions");
const apiKey = "59VNKM2L9TBEKL54GAS66S4W6";
const url = `https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/`;
searchBtn.addEventListener("click", (event) => {
const desiredLocation = searchBar.value.trim();
if (!desiredLocation) {
cityName.textContent = "Please enter a city name";
showText(cityName);
return;
}
searchWeather(desiredLocation);
cityName.textContent = desiredLocation;
});
async function searchWeather(location) {
try {
const response = await fetch(`${url}${location}?unitGroup=metric&key=${apiKey}&contentType=json`);
if(!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
console.log(response);
const data = await response.json();
displayWeather(data);
}
catch(error) {
console.log("Fetch error: ", error);
}
}
function displayWeather(data) {
const currentWeather = data.currentConditions;
showText(cityName);
weatherConditions.textContent = currentWeather.conditions;
showText(weatherConditions);
cityTemp.textContent = currentWeather.temp + "°C";
showText(cityTemp);
cityWind.textContent = "Wind speed: " + currentWeather.windspeed;
showText(cityWind);
humidity.textContent = "Humidity: " + currentWeather.humidity;
showText(humidity);
uvIndex.textContent = "UV Index: " + currentWeather.uvindex;
showText(uvIndex);
}
function showText(element) {
element.removeAttribute("hidden");
}