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 countryy = 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 ) ;
@@ -28,21 +28,22 @@ const updateWeatherInfo = (result) => {
2828 const { error, location, current } = result ;
2929 if ( error ) {
3030 cityName . innerText = `Error: ${ error . message } ` ;
31- [ country , dateTime , temp , humidity , condition2 , icon ] . forEach ( ( elem ) => {
32- elem . innerText = '' ;
31+ [ countryy , dateTime , temp , humidity , condition2 , icon ] . forEach ( ( elem ) => {
32+ elem . innerText = "" ;
3333 } ) ;
34- icon . src = '' ;
34+ icon . src = "" ;
3535 } else {
3636 const { name, country, localtime } = location ;
37+ console . log ( country ) ;
3738 cityName . innerText = name ;
38- country . innerText = country ;
39+ countryy . innerText = country ;
3940 dateTime . innerText = localtime ;
4041 temp . innerText = `${ current . temp_c } °C` ;
4142 humidity . innerText = `${ current . humidity } %` ;
4243 condition2 . innerText = current . condition . text ;
4344 icon . src = current . condition . icon ;
4445
45- const isDay = current . is_day === 1 ? ' day' : ' night' ;
46+ const isDay = current . is_day === 1 ? " day" : " night" ;
4647 const codes = [
4748 [ 1000 , 10000 , 10001 , 1100 , 11001 , 11000 , 51190 , 60030 ] , // clear
4849 [
@@ -77,37 +78,38 @@ const updateWeatherInfo = (result) => {
7778const getData = async ( cityName ) => {
7879 try {
7980 const result = await fetchData (
80- `https://api.weatherapi.com/v1/current.json?key=${ apiKey } &q=${ cityName } &aqi=no` ,
81+ `https://api.weatherapi.com/v1/current.json?key=${ apiKey } &q=${ cityName } &aqi=no`
8182 ) ;
8283 return result ;
8384 } catch ( error ) {
8485 return {
85- error : { message : ' Failed to fetch data. Please try again later.' } ,
86+ error : { message : " Failed to fetch data. Please try again later." } ,
8687 } ;
8788 }
8889} ;
89- const getlocation = async ( lat , long ) => fetchData (
90- `https://api.weatherapi.com/v1/current.json?key=${ apiKey } &q=${ lat } ,${ long } &aqi=no` ,
91- ) ;
90+ const getlocation = async ( lat , long ) =>
91+ fetchData (
92+ `https://api.weatherapi.com/v1/current.json?key=${ apiKey } &q=${ lat } ,${ long } &aqi=no`
93+ ) ;
9294
9395const gotlocation = async ( position ) => {
9496 try {
9597 const result = await getlocation (
9698 position . coords . latitude ,
97- position . coords . longitude ,
99+ position . coords . longitude
98100 ) ;
99101 console . log ( result ) ;
100102 updateWeatherInfo ( result ) ;
101103 } catch ( error ) {
102- cityName . innerText = ' Error fetching weather based on location' ;
104+ cityName . innerText = " Error fetching weather based on location" ;
103105 }
104106} ;
105- const failedlocation = ( ) => console . log ( ' failed to locate location' ) ;
107+ const failedlocation = ( ) => console . log ( " failed to locate location" ) ;
106108
107- btn . addEventListener ( ' click' , async ( e ) => {
109+ btn . addEventListener ( " click" , async ( e ) => {
108110 try {
109111 if ( input . value . length === 0 ) {
110- alert ( ' Please type a city name' ) ;
112+ alert ( " Please type a city name" ) ;
111113 } else {
112114 const { value } = input ;
113115 const result = await getData ( value ) ;
@@ -116,21 +118,23 @@ btn.addEventListener('click', async (e) => {
116118 console . log ( result ) ;
117119 }
118120 } catch ( error ) {
119- cityName . innerText = ' Error to fetch weather' ;
121+ cityName . innerText = " Error to fetch weather" ;
120122 }
121123 e . preventDefault ( ) ;
122124} ) ;
123125
124- locat . addEventListener ( 'click' , ( ) => navigator . geolocation . getCurrentPosition ( gotlocation , failedlocation ) ) ;
126+ locat . addEventListener ( "click" , ( ) =>
127+ navigator . geolocation . getCurrentPosition ( gotlocation , failedlocation )
128+ ) ;
125129const citiesArray = [ ...cities ] ;
126130citiesArray . forEach ( ( element ) => {
127- element . addEventListener ( ' click' , async ( ) => {
131+ element . addEventListener ( " click" , async ( ) => {
128132 const cityName = element . innerText ;
129133 const result = await getData ( cityName ) ;
130134 updateWeatherInfo ( result ) ;
131135 } ) ;
132136} ) ;
133137
134- window . addEventListener ( ' load' , async ( ) => {
138+ window . addEventListener ( " load" , async ( ) => {
135139 navigator . geolocation . getCurrentPosition ( gotlocation , failedlocation ) ;
136140} ) ;
0 commit comments