-
Notifications
You must be signed in to change notification settings - Fork 0
/
country.js
62 lines (54 loc) · 2.25 KB
/
country.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
const countryName = new URLSearchParams(location.search).get('name')
const flagImage = document.querySelector('.country-details img')
const countryNameH1 = document.querySelector('.country-details h1')
const nativeName = document.querySelector('.native-name')
const population = document.querySelector('.population')
const region = document.querySelector('.region')
const subRegion = document.querySelector('.sub-region')
const capital = document.querySelector('.capital')
const topLevelDomain = document.querySelector('.top-level-domain')
const currencies = document.querySelector('.currencies')
const languages = document.querySelector('.languages')
const borderCountries = document.querySelector('.border-countries')
fetch(`https://restcountries.com/v3.1/name/${countryName}?fullText=true`)
.then((res) => res.json())
.then(([country]) => {
flagImage.src = country.flags.svg
countryNameH1.innerText = country.name.common
population.innerText = country.population.toLocaleString('en-IN')
region.innerText = country.region
topLevelDomain.innerText = country.tld.join(', ')
if (country.capital) {
capital.innerText = country.capital?.[0]
}
if (country.subregion) {
subRegion.innerText = country.subregion
}
if (country.name.nativeName) {
nativeName.innerText = Object.values(country.name.nativeName)[0].common
} else {
nativeName.innerText = country.name.common
}
if (country.currencies) {
currencies.innerText = Object.values(country.currencies)
.map((currency) => currency.name)
.join(', ')
}
if (country.languages) {
languages.innerText = Object.values(country.languages).join(', ')
}
console.log(country);
if (country.borders) {
country.borders.forEach((border) => {
fetch(`https://restcountries.com/v3.1/alpha/${border}`)
.then((res) => res.json())
.then(([borderCountry]) => {
// console.log(borderCountry)
const borderCountryTag = document.createElement('a')
borderCountryTag.innerText = borderCountry.name.common
borderCountryTag.href = `country.html?name=${borderCountry.name.common}`
borderCountries.append(borderCountryTag)
})
})
}
})