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' ) ;
1414const fetchData = async ( url ) => {
1515 try {
1616 const data = await fetch ( url ) ;
@@ -27,11 +27,11 @@ const fetchData = async (url) => {
2727const updateWeatherInfo = ( result ) => {
2828 const { error, location, current } = result ;
2929 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 = '' ;
3535 } else {
3636 const { name, country, localtime } = location ;
3737 cityName . innerText = name ;
@@ -42,7 +42,7 @@ const updateWeatherInfo = (result) => {
4242 condition2 . innerText = current . condition . text ;
4343 icon . src = current . condition . icon ;
4444
45- const isDay = current . is_day == 1 ? " day" : " night" ;
45+ const isDay = current . is_day === 1 ? ' day' : ' night' ;
4646 const codes = [
4747 [ 1000 , 10000 , 10001 , 1100 , 11001 , 11000 , 51190 , 60030 ] , // clear
4848 [
@@ -65,7 +65,7 @@ const updateWeatherInfo = (result) => {
6565 `./images/${ isDay } /snowy.jpg` ,
6666 ] ;
6767
68- for ( let i = 0 ; i < codes . length ; i ++ ) {
68+ for ( let i = 0 ; i < codes . length ; i += 1 ) {
6969 if ( codes [ i ] . includes ( current . condition . code ) ) {
7070 body . style . backgroundImage = `url('${ imageUrls [ i ] } ')` ;
7171 console . log ( imageUrls [ i ] ) ;
@@ -77,38 +77,37 @@ const updateWeatherInfo = (result) => {
7777const getData = async ( cityName ) => {
7878 try {
7979 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` ,
8181 ) ;
8282 return result ;
8383 } catch ( error ) {
8484 return {
85- error : { message : " Failed to fetch data. Please try again later." } ,
85+ error : { message : ' Failed to fetch data. Please try again later.' } ,
8686 } ;
8787 }
8888} ;
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+ ) ;
9392
9493const gotlocation = async ( position ) => {
9594 try {
9695 const result = await getlocation (
9796 position . coords . latitude ,
98- position . coords . longitude
97+ position . coords . longitude ,
9998 ) ;
10099 console . log ( result ) ;
101100 updateWeatherInfo ( result ) ;
102101 } catch ( error ) {
103- cityName . innerText = " Error fetching weather based on location" ;
102+ cityName . innerText = ' Error fetching weather based on location' ;
104103 }
105104} ;
106- const failedlocation = ( ) => console . log ( " failed to locate location" ) ;
105+ const failedlocation = ( ) => console . log ( ' failed to locate location' ) ;
107106
108- btn . addEventListener ( " click" , async ( e ) => {
107+ btn . addEventListener ( ' click' , async ( e ) => {
109108 try {
110109 if ( input . value . length === 0 ) {
111- alert ( " Please type a city name" ) ;
110+ alert ( ' Please type a city name' ) ;
112111 } else {
113112 const { value } = input ;
114113 const result = await getData ( value ) ;
@@ -117,23 +116,21 @@ btn.addEventListener("click", async (e) => {
117116 console . log ( result ) ;
118117 }
119118 } catch ( error ) {
120- cityName . innerText = " Error to fetch weather" ;
119+ cityName . innerText = ' Error to fetch weather' ;
121120 }
122121 e . preventDefault ( ) ;
123122} ) ;
124123
125- locat . addEventListener ( "click" , ( ) =>
126- navigator . geolocation . getCurrentPosition ( gotlocation , failedlocation )
127- ) ;
124+ locat . addEventListener ( 'click' , ( ) => navigator . geolocation . getCurrentPosition ( gotlocation , failedlocation ) ) ;
128125const citiesArray = [ ...cities ] ;
129126citiesArray . forEach ( ( element ) => {
130- element . addEventListener ( " click" , async ( ) => {
127+ element . addEventListener ( ' click' , async ( ) => {
131128 const cityName = element . innerText ;
132129 const result = await getData ( cityName ) ;
133130 updateWeatherInfo ( result ) ;
134131 } ) ;
135132} ) ;
136133
137- window . addEventListener ( " load" , async ( ) => {
134+ window . addEventListener ( ' load' , async ( ) => {
138135 navigator . geolocation . getCurrentPosition ( gotlocation , failedlocation ) ;
139136} ) ;
0 commit comments