File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed
Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change 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+ } ) ;
You can’t perform that action at this time.
0 commit comments