-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
146 lines (111 loc) · 4.28 KB
/
script.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
/* Getting Elements by its id */
let check = document.getElementById("check");
let div = document.getElementById("show");
/* Adding Event Listener for button check */
check.addEventListener("click", () => {
div.innerHTML = ""; // Clearing the Previous data inside the div
let cityName = document.getElementById("name").value; // Getting value from the input bar
/* If the value from input bar is emptr it trigger a alert */
if (cityName == "") {
alert("Enter City Name");
}
/* Fetching weather api for current weather information */
const weatherapi = fetch(
`https://api.weatherapi.com/v1/current.json?key=6f8a57348e2946448cd73235242904&q=${cityName}&aqi=no`
);
weatherapi
.then((rdata) => rdata.json())
.then((data) => {
/* Assigning weather's values to a variable */
let name = data.location.name;
let region = data.location.region;
let country = data.location.country;
let update = data.current.last_updated;
let condition = data.current.condition.text;
let temp_c = data.current.temp_c;
let temp_f = data.current.temp_f;
let humidity = data.current.humidity;
let icon = data.current.condition.icon;
//console.log(data);
/* Fetching restcountries api to get some data's to match the value from weather api */
const restCountryApi = fetch("https://restcountries.com/v3.1/all");
restCountryApi
.then((rcdata) => rcdata.json())
.then((rcdata) => {
for (let i = 0; i < rcdata.length; i++) {
if (
country == rcdata[i].name.common ||
country == rcdata[i].name.official
) {
div.innerHTML = `
<div class="container body">
<h2 class="text-center">${name}'s Weather Status</h2>
<div class="row g-3 justify-content-center">
<div class="col">
<div class="card text-center h-100 ">
<div class="card-header">
<h5 class="card-title text-center">${name}</h5>
</div>
<div class="img-box">
<img src="${rcdata[i].flags.png}" class="card-img-top" alt="${rcdata[i].name.common}" />
</div>
<div class ="card-body">
<div class="card-text text-center">Region : ${region}</div>
<div class="card-text text-center">Capital : ${rcdata[i].capital} </div>
<div class="card-text text-center">Location : ${rcdata[i].name.common}, ${rcdata[i].cca2} </div>
</div>
<div class="card-footer">
<small class="text-body-primary">Last updated : ${update}</small>
</div>
</div>
</div>
<div class="col">
<div class="card text-center h-100">
<div class="card-header">
<h5 class="card-title text-center">Weather : ${condition} </h5>
</div>
<div class ="card-body">
<div class="table-responsive">
<img src="${icon}">
<table class="table custom-table" >
<thead>
<tr>
<th>Title</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<tr>
<td>Condition</td>
<td>${condition}</td>
</tr>
<tr>
<td>Temperature</td>
<td>${temp_c}°C/${temp_f}°F</td>
</tr>
<tr>
<td>Humity</td>
<td>${humidity}%</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="card-footer">
<small class="text-body-primary">Last updated : ${update}</small>
</div>
</div>
</div>
</div>
</div>
`;
}
}
})
.catch((error2) => console.log(error2));
})
.catch((error) =>
alert(`${cityName} is not found, Check the input format`, error)
);
document.getElementById("form").reset();
});