Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit fc136bb

Browse files
committed
add functionalities
1 parent f85a521 commit fc136bb

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

Source-Code/WeatherApp/script.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const input = document.getElementById("input");
2+
const btn = document.getElementById("btn");
3+
const apiKey = "e3a46268fdc2475cb63214712240202";
4+
const cityName = document.getElementById("city-name");
5+
const dateTime = document.getElementById("date-time");
6+
// const icon = document.getElementById('icon');
7+
const temp = document.getElementById("temp");
8+
const country = document.getElementById("country");
9+
const locat = document.getElementById("getlocation");
10+
11+
const fetchData = async (url) => {
12+
try {
13+
const data = await fetch(url);
14+
if (!data.ok) {
15+
throw new Error(data.statusText);
16+
}
17+
return data.json();
18+
} catch (error) {
19+
console.log(error);
20+
throw error;
21+
}
22+
};
23+
const updateWeatherInfo = (result) => {
24+
cityName.innerText = `${result.location.name}`;
25+
country.innerText = `${result.location.country}`;
26+
dateTime.innerText = `${result.location.localtime}`;
27+
temp.innerText = `${result.current.temp_c} °C`;
28+
};
29+
const getData = async (cityName) =>
30+
fetchData(
31+
`http://api.weatherapi.com/v1/current.json?key=${apiKey}&q=${cityName}&aqi=no`
32+
);
33+
34+
btn.addEventListener("click", async () => {
35+
try {
36+
const { value } = input;
37+
const result = await getData(value);
38+
updateWeatherInfo(result);
39+
} catch (error) {
40+
cityName.innerText = "Error to fetch weather";
41+
}
42+
});

0 commit comments

Comments
 (0)