1
- //Selection of Elements
2
- const currentWeather = document . querySelector ( ".current-weather" ) ;
3
- const weatherForeCast = document . querySelector ( ".weather-forecast" ) ;
4
- let cityName = document . querySelector ( "input" ) ;
5
- const button = document . querySelector ( "button" ) ;
6
- const para = document . querySelector ( ".currTime" ) ;
7
- const extraInfo = document . querySelector ( ".extraInfo" ) ;
8
- const temp = document . querySelector ( ".temp" ) ;
9
- const inputVal = document . querySelector ( ".inputVal" ) ;
10
- const description = document . querySelector ( ".description" ) ;
11
1
const API = "e594f5869719e24b7089dcc31a97c76f" ; //Put your API in here
2
+ let cityName = document . querySelector ( "input" ) ;
12
3
13
- const dataFecth = async function ( ) {
4
+ async function dataFecth ( ) {
14
5
try {
15
6
const response = await fetch (
16
7
`https://api.openweathermap.org/data/2.5/weather?q=${ cityName . value } &appid=${ API } `
@@ -23,7 +14,7 @@ const dataFecth = async function () {
23
14
} catch ( error ) {
24
15
console . error ( "Error aa gya:" , error ) ;
25
16
}
26
- } ;
17
+ }
27
18
28
19
async function fetchForeCast ( ) {
29
20
try {
@@ -41,41 +32,25 @@ async function fetchForeCast() {
41
32
}
42
33
}
43
34
44
- function getTime ( ) {
45
- let time = new Date ( ) ;
46
- let IndiaTime = time . toLocaleTimeString ( "en-In" , {
47
- timeZone : "Asia/Kolkata" ,
48
- } ) ;
49
- return IndiaTime ;
50
- }
51
- function startInterval ( ) {
52
- setInterval ( ( ) => {
53
- document . querySelector ( "#getTime" ) . textContent = getTime ( ) ;
54
- } , 1000 ) ;
55
- }
56
-
57
- button . addEventListener ( "click" , ( ) => {
35
+ document . querySelector ( "button" ) . addEventListener ( "click" , ( ) => {
58
36
dataFecth ( ) ;
59
- startInterval ( ) ;
60
37
fetchForeCast ( ) ;
61
38
} ) ;
62
39
63
40
function displayCurrentData ( data ) {
64
- const p = document . createElement ( "p" ) ;
65
- const img = document . createElement ( "img" ) ;
66
- const mT1 = document . createElement ( "p" ) ;
67
- const mT2 = document . createElement ( "p" ) ;
68
- const C = document . createElement ( "span" ) ;
69
- C . textContent = Math . round ( data ?. main ?. temp - 273.15 ) + " °C" ;
70
- p . textContent = "Current Weather" ;
71
- img . src = `https://openweathermap.org/img/wn/${ data . weather [ 0 ] . icon } .png` ;
72
- mT1 . textContent = data . weather [ 0 ] . main ;
73
- mT2 . textContent = `Feels Like ${ Math . round (
74
- data . main . feels_like - 273.15
75
- ) } °C`;
76
- para . insertBefore ( p , para . firstChild ) ;
77
- extraInfo . append ( mT1 , mT2 ) ;
78
- temp . append ( img , C ) ;
41
+ const currentWeather = document . querySelector ( ".current-weather" ) ;
42
+ currentWeather . innerHTML = `
43
+ <p id="currTime">Current Weather</p>
44
+ <div class="temp">
45
+ <img src="https://openweathermap.org/img/wn/${ data . weather [ 0 ] . icon } .png">
46
+ <span>${ Number ( data ?. main ?. temp - 273.15 ) . toFixed ( 2 ) } °C</span>
47
+ </div>
48
+ <div class="extraInfo">
49
+ <p>${ data . weather [ 0 ] . main } </p>
50
+ <p>Feels Like ${ Number ( data . main . feels_like - 273.15 ) . toFixed ( 2 ) } °C</p>
51
+ </div>
52
+ ` ;
53
+ const inputVal = document . querySelector ( ".inputVal" ) ;
79
54
inputVal . textContent = cityName . value ;
80
55
}
81
56
@@ -88,23 +63,23 @@ function getForeastData(data) {
88
63
}
89
64
90
65
function displayForeCast ( data ) {
66
+ const weatherForeCast = document . querySelector ( ".weather-forecast" ) ;
67
+ const description = document . querySelector ( ".description" ) ;
91
68
const p = document . createElement ( "p" ) ;
92
69
p . id = "check" ;
93
70
p . textContent = `5 Day Weather Forecast for city ${ cityName . value } ` ;
94
71
description . appendChild ( p ) ;
95
72
for ( let i = 0 ; i < data . length ; i ++ ) {
96
- const div = document . createElement ( "div" ) ;
97
- const temp = document . createElement ( "p" ) ;
98
- const date = document . createElement ( "p" ) ;
99
- const img = document . createElement ( "img" ) ;
100
- date . textContent = new Date ( data [ i ] . dt * 1000 ) . toLocaleDateString ( "en-In" , {
73
+ let date = new Date ( data [ i ] . dt * 1000 ) . toLocaleDateString ( "en-In" , {
101
74
day : "numeric" ,
102
75
month : "short" ,
103
76
} ) ;
104
- img . src = `https://openweathermap.org/img/wn/${ data [ i ] . weather [ 0 ] . icon } .png` ;
105
- temp . textContent = Number ( data [ i ] ?. main ?. temp - 273.15 ) . toFixed ( 2 ) + " °C" ;
106
- div . id = "detail" ;
107
- div . append ( date , img , temp ) ;
108
- weatherForeCast . append ( div ) ;
77
+ weatherForeCast . innerHTML += `
78
+ <div id="detail">
79
+ <p>${ date } </p>
80
+ <img src="${ `https://openweathermap.org/img/wn/${ data [ i ] . weather [ 0 ] . icon } .png` } ">
81
+ <p>${ Number ( data [ i ] ?. main ?. temp - 273.15 ) . toFixed ( 2 ) + "°C" } </p>
82
+ </div>
83
+ ` ;
109
84
}
110
85
}
0 commit comments