-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
178 lines (146 loc) · 7.97 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Yzg2N2U2MzEtODUwMC00YmMyLThjZWMtM2UxMThlMWU1MDFj API token number
// Api Key : f7a60a2c5bef423690c135010221512
const DefaultPlace = "India";
const CheckCity = /^[A-Za-z]+$/;
let WeatherContainer = document.querySelector(".Weather_Container")
let CurrentDate = document.getElementById("Date");
let CurrentTime = document.getElementById("Time");
let City = document.getElementById("Place_Name");
let SearchCity = document.getElementById("Search_Place");
let SearchButton = document.getElementById('SearchButton');
let Temperature = document.getElementById("Temperature");
let Condition = document.getElementById("Condition");
let Humidity = document.getElementById("Humidity");
let WindSpeed = document.getElementById("WindSpeed");
let ErrorMsg = document.getElementById("ErrorMsg");
function displaycity(SearchCity){
var c1=document.getElementById("Search_Place").value;
document.getElementById("displayArea").innerHTML="you entered: "+c1;
}
let Day1= document.getElementById("Day1")
let Day1Temperature = document.getElementById("Day1Temperature");
let Day1Humidity = document.getElementById("Day1Humidity");
let Day1Wind = document.getElementById("Day1Wind");
let Day1ConditionBox = document.getElementById("Day1ConditionBox");
let Day2= document.getElementById("Day2")
let Day2Temperature = document.getElementById("Day2Temperature");
let Day2Humidity = document.getElementById("Day2Humidity");
let Day2Wind = document.getElementById("Day2Wind");
let Day2ConditionBox = document.getElementById("Day2ConditionBox");
let Day3= document.getElementById("Day3")
let Day3ConditionBox = document.getElementById("Day3ConditionBox");
let Day3Temperature = document.getElementById("Day3Temperature");
let Day3Humidity = document.getElementById("Day3Humidity");
let Day3Wind = document.getElementById("Day3Wind");
let Day4= document.getElementById("Day4")
let Day4ConditionBox = document.getElementById("Day4ConditionBox");
let Day4Temperature = document.getElementById("Day4Temperature");
let Day4Humidity = document.getElementById("Day4Humidity");
let Day4Wind = document.getElementById("Day4Wind");
let Day5= document.getElementById("Day5")
let Day5ConditionBox = document.getElementById("Day5ConditionBox");
let Day5Temperature = document.getElementById("Day5Temperature");
let Day5Humidity = document.getElementById("Day5Humidity");
let Day5Wind = document.getElementById("Day5Wind");
let Day6= document.getElementById("Day6")
let Day6ConditionBox = document.getElementById("Day6ConditionBox");
let Day6Temperature = document.getElementById("Day6Temperature");
let Day6Humidity = document.getElementById("Day6Humidity");
let Day6Wind = document.getElementById("Day6Wind");
let ConditionIconBox = document.getElementById("ConditionIcon");
SearchButton.addEventListener('click', (event)=>{
let len= SearchCity.value
if(SearchCity.value.match(CheckCity) && len.length != 0){
GetWeather(SearchCity.value);
}
else{
ErrorMsg.style.opacity = 1;
ErrorMsg.innerHTML ="Kindly Enter Valid Place Name !";
ErrorMsg.style.backgroundColor ="red";
HideErrorMsg();
SearchCity.focus();
SearchCity.value="";
}
})
const GetWeather = (city) =>{
const url = `http://api.weatherapi.com/v1/forecast.json?key=378603cebc014f298d5164956232702&q=${city}&days=6&aqi=no&alerts=no`;
fetch(url).then((resp) => resp.json())
.then(data =>{
ErrorMsg.style.opacity = 1;
ErrorMsg.style.backgroundColor ="green";
ErrorMsg.innerHTML =`${city}'s Weather Details Show`;
HideErrorMsg();
return ShowWeather(data)
})
.catch(()=>{
ErrorMsg.style.opacity = 1;
ErrorMsg.style.backgroundColor ="red";
ErrorMsg.innerHTML =`${city} not found`;
SearchCity.focus();
SearchCity.value = "";
HideErrorMsg();
})
}
const ShowWeather = (WeatherData) =>{
//! If there is any error remove the comment from console.log(WeatherData); to look for the data which comes from the API
// console.log(WeatherData);
City.innerHTML = `${WeatherData.location.country}`;
Temperature.innerHTML =`${WeatherData.current.temp_c}`;
Condition.innerHTML = `${WeatherData.current.condition.text}`;
Humidity.innerHTML = `${WeatherData.current.humidity}`;
WindSpeed.innerHTML = `${WeatherData.current.wind_kph}`;
ConditionIconBox.innerHTML =`<img src="${WeatherData.current.condition.icon}" alt="Condition-Icon">`
Day1.innerHTML = `${WeatherData.forecast.forecastday[1].date}`;
Day1ConditionBox.innerHTML = `<p>Condition<span>${WeatherData.forecast.forecastday[1].day.condition.text}</span> </p>
<img src="${WeatherData.forecast.forecastday[1].day.condition.icon}" alt="Condition-Icon">`
Day1Temperature.innerHTML = `${WeatherData.forecast.forecastday[1].day.avgtemp_c}`+"°C" ;
Day1Humidity.innerHTML = `${WeatherData.forecast.forecastday[1].day.avghumidity}` + "%";
Day1Wind.innerHTML = `${WeatherData.forecast.forecastday[1].day.maxtemp_c}` + "Km/h";
Day2.innerHTML = `${WeatherData.forecast.forecastday[2].date}`;
Day2ConditionBox.innerHTML = `<p>Condition<span>${WeatherData.forecast.forecastday[2].day.condition.text}</span> </p>
<img src="${WeatherData.forecast.forecastday[2].day.condition.icon}" alt="Condition-Icon">`
Day2Temperature.innerHTML = `${WeatherData.forecast.forecastday[2].day.avgtemp_c}`+"°C" ;
Day2Humidity.innerHTML = `${WeatherData.forecast.forecastday[2].day.avghumidity}` + "%";
Day2Wind.innerHTML = `${WeatherData.forecast.forecastday[2].day.maxtemp_c}` + "Km/h";
Day3.innerHTML = `${WeatherData.forecast.forecastday[3].date}`;
Day3ConditionBox.innerHTML = `<p>Condition<span>${WeatherData.forecast.forecastday[3].day.condition.text}</span> </p>
<img src="${WeatherData.forecast.forecastday[3].day.condition.icon}" alt="Condition-Icon">`
Day3Temperature.innerHTML = `${WeatherData.forecast.forecastday[3].day.avgtemp_c}`+"°C" ;
Day3Humidity.innerHTML = `${WeatherData.forecast.forecastday[3].day.avghumidity}` + "%";
Day3Wind.innerHTML = `${WeatherData.forecast.forecastday[3].day.maxtemp_c}` + "Km/h";
Day4.innerHTML = `${WeatherData.forecast.forecastday[4].date}`;
Day4ConditionBox.innerHTML = `<p>Condition<span>${WeatherData.forecast.forecastday[4].day.condition.text}</span> </p>
<img src="${WeatherData.forecast.forecastday[4].day.condition.icon}" alt="Condition-Icon">`
Day4Temperature.innerHTML = `${WeatherData.forecast.forecastday[4].day.avgtemp_c}`+"°C" ;
Day4Humidity.innerHTML = `${WeatherData.forecast.forecastday[4].day.avghumidity}` + "%";
Day4Wind.innerHTML = `${WeatherData.forecast.forecastday[4].day.maxtemp_c}` + "Km/h";
Day5.innerHTML = `${WeatherData.forecast.forecastday[5].date}`;
Day5ConditionBox.innerHTML = `<p>Condition<span>${WeatherData.forecast.forecastday[5].day.condition.text}</span> </p>
<img src="${WeatherData.forecast.forecastday[5].day.condition.icon}" alt="Condition-Icon">`
Day5Temperature.innerHTML = `${WeatherData.forecast.forecastday[5].day.avgtemp_c}`+"°C" ;
Day5Humidity.innerHTML = `${WeatherData.forecast.forecastday[5].day.avghumidity}` + "%";
Day5Wind.innerHTML = `${WeatherData.forecast.forecastday[5].day.maxtemp_c}` + "Km/h";
};
let SystemDate = new Date();
CurrentDate.innerHTML = SystemDate.getDate()+"/"+ (SystemDate.getMonth()+1) +"/"+ SystemDate.getFullYear();
function startTime() {
const today = new Date();
let h = today.getHours();
let m = today.getMinutes();
let s = today.getSeconds();
m = checkTime(m);
s = checkTime(s);
CurrentTime.innerHTML = h + ":" + m + ":" + s;
setTimeout(startTime, 1000);
}
function checkTime(i) {
if (i < 10) {
i = "0" + i
};
return i;
}
function HideErrorMsg() {
setTimeout(() => {
ErrorMsg.style.opacity = 0;
}, 5000);
}