-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
94 lines (79 loc) · 3.16 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
"use strict";
//Globaalit
var country_source_url = "https://restcountries.eu/rest/v2/all";
var all_countries = [];
var kaydyt_maat = [];
var vaihtoehdot = [];
function init() {
fetch(
country_source_url
).then(
function(response) {
return response.json();
}
).then(
function(list) {
all_countries.push(...list);
var suitable = _.filter(
all_countries,
function (country) {
return country.borders.length >= 2;
}
)
var randomCountries = [
suitable.splice(Math.floor(Math.random()*suitable.length),1)[0],
suitable.splice(Math.floor(Math.random()*suitable.length),1)[0],
suitable.splice(Math.floor(Math.random()*suitable.length),1)[0]
]
showCountries(randomCountries);
}
)
}
function showCountries(countries){
countries.forEach(
function(country) {
let countryName = country["name"]
let currencies = country.currencies.map(
function(a) {
return a.name + " (" + a.code + ") "
}
)
let flagURL = country.flag;
let maa = document.createElement("div");
maa.setAttribute('id', countryName+'-country');
maa.setAttribute('class', 'maa');
let countrySelector = document.createElement("input");
let selectionName = countryName + "-button";
countrySelector.setAttribute('type','button');
countrySelector.setAttribute('name',selectionName);
countrySelector.setAttribute('value',countryName);
countrySelector.setAttribute('id',selectionName);
countrySelector.setAttribute('class', 'countryselector');
let countryDescription = document.createElement("p");
countryDescription.setAttribute('id', countryName + "-nametext");
countryDescription.setAttribute('class', 'countrydescription');
let flag = document.createElement("img");
flag.setAttribute('src', flagURL);
flag.setAttribute('alt', "Flag of " + countryName);
flag.setAttribute('heigth', 100);
flag.setAttribute('width', 200);
flag.setAttribute('class', 'countryflag');
maa.appendChild(countryDescription);
maa.appendChild(flag);
maa.appendChild(countrySelector);
document.querySelector("p.maat").appendChild(maa);
document.getElementById(countryName+"-nametext").innerHTML =
"Name: " + countryName + "<br>" +
"Code: " + country.alpha3Code + "<br>" +
"Capital: " + country.capital + "<br>" +
"Currencies: " + currencies + "<br>" +
"Borders: " + country.borders + "<br>" +
"";
document.getElementById(selectionName).onclick = printCountryNeighbors;
}
)
}
function printCountryNeighbors(country){
console.log(country.target.value);
}
init();