-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
86 lines (70 loc) · 2.66 KB
/
scripts.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
let searchBtn = document.getElementById('search-btn');
let countryInp = document.getElementById('country-inp');
searchBtn.addEventListener('click', () => {
let countryName = countryInp.value;
// let countryName = "India";
let finalURL = `https://restcountries.com/v3.1/name/${countryName}?fullText=true`;
console.log(finalURL);
//* get data form the fetch api
fetch(finalURL).then((response) => response.json())
.then((data) => {
console.log(data[0]);
console.log(data[0].capital[0]);
console.log(data[0].flags.svg);
console.log(data[0].name.common);
console.log(data[0].continents[0]);
console.log(Object.keys(data[0].currencies)[0]);
console.log(data[0].currencies[Object.keys(data[0].currencies)].name);
console.log(Object.values(data[0].languages).toString().split(",").join(","));
console.log(data[0].population);
console.log(data[0].maps.googleMaps);
result.innerHTML = `
<img src="${data[0].flags.svg}"
class="flag-img">
<h2>${data[0].name.common}</h2>
<div class=" wrapper">
<div class="data-wrapper">
<h4>Capital:</h4>
<span>${data[0].capital[0]}</span>
</div>
</div>
<div class=" wrapper">
<div class="data-wrapper">
<h4>Continent:</h4>
<span>${data[0].continents[0]}</span>
</div>
</div>
<div class=" wrapper">
<div class="data-wrapper">
<h4>Population:</h4>
<span>${data[0].population}</span>
</div>
</div>
<div class=" wrapper">
<div class="data-wrapper">
<h4>Currencies:</h4>
<span>${data[0].currencies[Object.keys(data[0].currencies)].name}</span>
</div>
</div>
<div class=" wrapper">
<div class="data-wrapper">
<h4>Language:</h4>
<span>${Object.values(data[0].languages).toString().split(",").join(",")}</span>
</div>
</div>
`
})
})
// .catch(()=> {
// if(countryName.lenghth == 0) {
// result.innerHTML =`<h3> The input field cannot be empty </h3>`
// }else{
// result.innerHTML =`<h3> Please enter a valid country name </h3>`
// }
// })
// <div class=" wrapper">
// <div class="data-wrapper">
// <h4>Maps:</h4>
// window.location.href = "${data[0].maps.googleMaps}";
// </div>
// </div>