-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
120 lines (66 loc) · 2.27 KB
/
app.js
File metadata and controls
120 lines (66 loc) · 2.27 KB
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
//Declaring all Variables
const h2 = document.querySelector(".h2")
const confirme = document.querySelector(".confirmed h4")
const recovered = document.querySelector(".recovered h4")
const deaths = document.querySelector(".deaths h4")
const select = document.querySelector("#country")
//Declaring API urls for app
var dataUrl = "https://covid19.mathdro.id/api"
const countryUrl = `${dataUrl}`+"/"+"countries"
let anotherdataUrl = "https://covid19.mathdro.id/api"
//5:This is the Last Function to Call All Async Functions
async function allCall(){
let datas= await data()
let confi = await confirmed()
let countr = await country()
}
allCall().catch(err => h2.innerHTML = "check your connection")
//1: Get the data from API url and change into JSON
async function data (){
if(dataUrl === anotherdataUrl){
const response = await fetch(anotherdataUrl)
const json = await response.json()
return json
}else {
let respons = await fetch(anotherdataUrl)
let anres= await respons
let anofet=await anres.url.replace("%20","")
let fet = await fetch(anofet)
let js = await fet.json()
return js
}
};
//2:Get the cases From that JSON(which is Fetched in 1)
async function confirmed(){
let charan = await data()
let noc = await charan.confirmed.value
let nor=await charan.recovered.value
let noi = await charan.deaths.value
confirme.innerHTML = noc
recovered.innerHTML = nor
deaths.innerHTML = noi
}
//3 Get the Every Countriy with different API url
//3:1 Create the "SELECT" HTML element
//3:2 Create "OPTION" HTML element and append every Country from the APi url
async function country()
{
let data = await fetch(countryUrl)
let arr = await data.json()
let another = await arr.countries
another.forEach(country =>{
let option= document.createElement("option")
option.append(country.name)
select.addEventListener("change",changeCountry)
select.appendChild(option)
})
}
//4:This Function needs to call EveryTime we change the Country
function changeCountry(){
let change = document.querySelector("#country").value
h2.innerHTML = change
var dataCountry = `${countryUrl} `+ "/"+`${change}`
anotherdataUrl = dataCountry
console.log(anotherdataUrl)
allCall()
}