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 condition2 = document . getElementById ( " condition2" ) ;
7
- const temp = document . getElementById ( " temp" ) ;
8
- const humidity = document . getElementById ( " humidity" ) ;
9
- const country = document . getElementById ( " country" ) ;
10
- const locat = document . getElementById ( " getlocation" ) ;
11
- const cities = document . getElementsByClassName ( " city" ) ;
12
- const icon = document . getElementById ( " icon" ) ;
13
- const body = document . querySelector ( " .weather-app" ) ;
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 condition2 = document . getElementById ( ' condition2' ) ;
7
+ const temp = document . getElementById ( ' temp' ) ;
8
+ const humidity = document . getElementById ( ' humidity' ) ;
9
+ const country = document . getElementById ( ' country' ) ;
10
+ const locat = document . getElementById ( ' getlocation' ) ;
11
+ const cities = document . getElementsByClassName ( ' city' ) ;
12
+ const icon = document . getElementById ( ' icon' ) ;
13
+ const body = document . querySelector ( ' .weather-app' ) ;
14
14
const fetchData = async ( url ) => {
15
15
try {
16
16
const data = await fetch ( url ) ;
@@ -27,11 +27,11 @@ const fetchData = async (url) => {
27
27
const updateWeatherInfo = ( result ) => {
28
28
const { error, location, current } = result ;
29
29
if ( error ) {
30
- cityName . innerText = " Error: " + error . message ;
31
- [ country , dateTime , temp , humidity , condition2 , icon ] . forEach (
32
- ( elem ) => ( elem . innerText = "" )
33
- ) ;
34
- icon . src = "" ;
30
+ cityName . innerText = ` Error: ${ error . message } ` ;
31
+ [ country , dateTime , temp , humidity , condition2 , icon ] . forEach ( ( elem ) => {
32
+ elem . innerText = '' ;
33
+ } ) ;
34
+ icon . src = '' ;
35
35
} else {
36
36
const { name, country, localtime } = location ;
37
37
cityName . innerText = name ;
@@ -42,7 +42,7 @@ const updateWeatherInfo = (result) => {
42
42
condition2 . innerText = current . condition . text ;
43
43
icon . src = current . condition . icon ;
44
44
45
- const isDay = current . is_day == 1 ? " day" : " night" ;
45
+ const isDay = current . is_day === 1 ? ' day' : ' night' ;
46
46
const codes = [
47
47
[ 1000 , 10000 , 10001 , 1100 , 11001 , 11000 , 51190 , 60030 ] , // clear
48
48
[
@@ -65,7 +65,7 @@ const updateWeatherInfo = (result) => {
65
65
`./images/${ isDay } /snowy.jpg` ,
66
66
] ;
67
67
68
- for ( let i = 0 ; i < codes . length ; i ++ ) {
68
+ for ( let i = 0 ; i < codes . length ; i += 1 ) {
69
69
if ( codes [ i ] . includes ( current . condition . code ) ) {
70
70
body . style . backgroundImage = `url('${ imageUrls [ i ] } ')` ;
71
71
console . log ( imageUrls [ i ] ) ;
@@ -77,38 +77,37 @@ const updateWeatherInfo = (result) => {
77
77
const getData = async ( cityName ) => {
78
78
try {
79
79
const result = await fetchData (
80
- `http://api.weatherapi.com/v1/current.json?key=${ apiKey } &q=${ cityName } &aqi=no`
80
+ `http://api.weatherapi.com/v1/current.json?key=${ apiKey } &q=${ cityName } &aqi=no` ,
81
81
) ;
82
82
return result ;
83
83
} catch ( error ) {
84
84
return {
85
- error : { message : " Failed to fetch data. Please try again later." } ,
85
+ error : { message : ' Failed to fetch data. Please try again later.' } ,
86
86
} ;
87
87
}
88
88
} ;
89
- const getlocation = async ( lat , long ) =>
90
- fetchData (
91
- `http://api.weatherapi.com/v1/current.json?key=${ apiKey } &q=${ lat } ,${ long } &aqi=no`
92
- ) ;
89
+ const getlocation = async ( lat , long ) => fetchData (
90
+ `http://api.weatherapi.com/v1/current.json?key=${ apiKey } &q=${ lat } ,${ long } &aqi=no` ,
91
+ ) ;
93
92
94
93
const gotlocation = async ( position ) => {
95
94
try {
96
95
const result = await getlocation (
97
96
position . coords . latitude ,
98
- position . coords . longitude
97
+ position . coords . longitude ,
99
98
) ;
100
99
console . log ( result ) ;
101
100
updateWeatherInfo ( result ) ;
102
101
} catch ( error ) {
103
- cityName . innerText = " Error fetching weather based on location" ;
102
+ cityName . innerText = ' Error fetching weather based on location' ;
104
103
}
105
104
} ;
106
- const failedlocation = ( ) => console . log ( " failed to locate location" ) ;
105
+ const failedlocation = ( ) => console . log ( ' failed to locate location' ) ;
107
106
108
- btn . addEventListener ( " click" , async ( e ) => {
107
+ btn . addEventListener ( ' click' , async ( e ) => {
109
108
try {
110
109
if ( input . value . length === 0 ) {
111
- alert ( " Please type a city name" ) ;
110
+ alert ( ' Please type a city name' ) ;
112
111
} else {
113
112
const { value } = input ;
114
113
const result = await getData ( value ) ;
@@ -117,23 +116,21 @@ btn.addEventListener("click", async (e) => {
117
116
console . log ( result ) ;
118
117
}
119
118
} catch ( error ) {
120
- cityName . innerText = " Error to fetch weather" ;
119
+ cityName . innerText = ' Error to fetch weather' ;
121
120
}
122
121
e . preventDefault ( ) ;
123
122
} ) ;
124
123
125
- locat . addEventListener ( "click" , ( ) =>
126
- navigator . geolocation . getCurrentPosition ( gotlocation , failedlocation )
127
- ) ;
124
+ locat . addEventListener ( 'click' , ( ) => navigator . geolocation . getCurrentPosition ( gotlocation , failedlocation ) ) ;
128
125
const citiesArray = [ ...cities ] ;
129
126
citiesArray . forEach ( ( element ) => {
130
- element . addEventListener ( " click" , async ( ) => {
127
+ element . addEventListener ( ' click' , async ( ) => {
131
128
const cityName = element . innerText ;
132
129
const result = await getData ( cityName ) ;
133
130
updateWeatherInfo ( result ) ;
134
131
} ) ;
135
132
} ) ;
136
133
137
- window . addEventListener ( " load" , async ( ) => {
134
+ window . addEventListener ( ' load' , async ( ) => {
138
135
navigator . geolocation . getCurrentPosition ( gotlocation , failedlocation ) ;
139
136
} ) ;
0 commit comments