-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
128 lines (100 loc) · 3.85 KB
/
app.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// country name and its currency code
let country_details;
async function fetchCountryInfo() {
try {
const response = await fetch('https://restcountries.com/v3.1/all');
const countries = await response.json();
const countryInfo = {};
countries.forEach(country => {
const name = country.name.common;
const currency = country.currencies ? Object.keys(country.currencies)[0] : 'N/A';
const countryCode = country.cca2 || 'N/A';
countryInfo[name] = {
currency: currency,
countryCode: countryCode
};
});
return countryInfo;
} catch (error) {
console.error('Error fetching country information:', error);
return null;
}
}
fetchCountryInfo().then(countryInfo => {
country_details=countryInfo
makeoption(Object.keys(countryInfo))
});
// ----------------making availabe of all country name in select tag----------------------------------
function makeoption(country_names){
let from_country_option=document.querySelector("#from")
let to_country_option=document.querySelector("#to")
for(country of country_names.sort()){
from_country_option.innerHTML+=`<option value=${country}>${country}</option>`
to_country_option.innerHTML+=`<option value=${country}>${country}</option>`
}
getResponse()
}
// -------------------------- getting user input------------------------------------------
function getResponse(){
document.querySelector(".convert-btn").addEventListener("click",()=>{
document.querySelector(".all-details-container").innerHTML=""
let from=document.querySelector(".from")
let to=document.querySelector(".to")
let amt=document.querySelector(".amt").value
// fetching data
const api_key="748805839d55f74fa0d8d6ea"
fetch(`https://v6.exchangerate-api.com/v6/${api_key}/latest/${country_details[country].currency}`)
.then(res=>res.json())
.then((data)=>{
let from_country_currency=country_details[from.value].currency
let to_country_currency=country_details[to.value].currency
displayAllcountry(data["conversion_rates"],from_country_currency,to_country_currency)
displayMain(data["conversion_rates"],amt,from_country_currency,to_country_currency)
})
})
}
// main display
function displayMain(data,amt,from,to){
amt=amt==""?1:parseInt(amt)
let from_val=amt
let to_val=(amt*data[to]).toFixed(2)
if(from==to){
to_val=from_val
}
document.querySelector(".output-container").innerHTML=
`
<h1 class="output">${amt+" "+from.toLowerCase()+" "+" = "+" "+to_val+" "+to.toLowerCase()}</h1>
`
}
// all country display
function displayAllcountry(data_list,from,to){
let container=document.querySelector(".all-details-container")
for(country in country_details){
let val=data_list[country_details[country]["currency"]]
let country_code=country_details[country]["countryCode"]
let def="IN"
let flag_img=`https://flagsapi.com/${country_code}`
let elem=
`
<div class="col-sm-4 col-md-6 col-lg-2">
<div class="detail">
<img src="https://flagsapi.com/${flag_img}/flat/64.png" alt="${country}">
<p>${country.toUpperCase()}</p>
<p>1${" "+from}</p>
<p>=</p>
<p>${data_list[country_details[country]["currency"]]!=undefined?data_list[country_details[country]["currency"]]:"NA"}${" "+country_details[country]["currency"]}</p>
</div>
</div>
`
container.innerHTML+=elem
}
}
// interchange the exchange option
document.querySelector(".exchange-btn").addEventListener("click",()=>{
let from=document.querySelector(".from")
let to=document.querySelector(".to")
let from_cur_value=from.value
let to_cur_value=to.value
from.value=to_cur_value
to.value=from_cur_value
})