-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhase_6.js
95 lines (73 loc) · 2.63 KB
/
Phase_6.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
const list = document.getElementById("countries");
let countries;
let selectedCode;
list.addEventListener("change", newSelection);
function newSelection(event) {
displayCountryInfo(event.target.value);
}
fetch("https://restcountries.eu/rest/v2/all")
.then(function (res) {
return res.json();
})
.then(function (data) {
initialize(data);
})
.catch(function (err) {
console.log("Error:", err);
});
function initialize(Data) {
countries = Data;
let options = "";
countries.forEach(country => options += `<option value="${country.alpha3Code}">${country.name}</option>`);
list.innerHTML = options;
list.selectedIndex = Math.floor(Math.random() * list.length);
displayCountryInfo(list[list.selectedIndex].value);
}
function displayCountryInfo(Alpha3Code) {
selectedCode = Alpha3Code;
const countryData = countries.find(country => country.alpha3Code === Alpha3Code);
document.querySelector("#flag-container img").src = countryData.flag;
document.querySelector("#flag-container img").alt = `Flag of ${countryData.name}`;
}
fetch("https://restcountries.eu/rest/v2/all")
.then(function (res) {
return res.json();
})
.then(function (data) {
initialize(data);
})
.catch(function (err) {
console.log("Error:", err);
});
function Capital() {
const countryData = countries.find(country => country.alpha3Code === selectedCode);
document.getElementById("capital").innerHTML = countryData.capital;
}
fetch("https://restcountries.eu/rest/v2/all")
.then(function(res){
return res.json();
})
.then(function(data){
initialize(data);
})
.catch(function(err){
console.log("Error:", err);
});
function Currency(){
const countryData = countries.find(country => country.alpha3Code === selectedCode);
document.getElementById("currency").innerHTML = countryData.currencies.filter(c => c.name).map(c => `${c.name} (${c.code})`).join(", ");
}
fetch("https://restcountries.eu/rest/v2/all")
.then(function(res){
return res.json();
})
.then(function(data){
initialize(data);
})
.catch(function(err){
console.log("Error:", err);
});
function Population(){
const countryData = countries.find(country => country.alpha3Code === selectedCode);
document.getElementById("population").innerHTML = countryData.population.toLocaleString("en-US");
}