-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
82 lines (65 loc) · 2.5 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
// const base_url= "https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@latest/v1/currencies"
const api= 'https://v6.exchangerate-api.com/v6/278fd3cc0fe9cb0f0cf5f42b/latest/USD';
const dropdowns= document.querySelectorAll(".dropdown select");
const btn= document.querySelector('form button');
const fromcurr= document.querySelector(".from select");
const tocurr= document.querySelector(".to select");
const msg= document.querySelector(".msg");
for(let select of dropdowns ){
for(let currcode in countryList){
let newoption= document.createElement("option");
newoption.innerText= currcode;
newoption.value= currcode;
select.append(newoption);
if(select.name==="from" && currcode==="USD")
newoption.selected="selected";
else if(select.name==="to" && currcode==="INR")
newoption.selected= "selected";
select.append(newoption);
}
select.addEventListener("change",(evt)=>{
updateflag(evt.target);
});
}
const updateExchangeRate = ()=>{
let amount= document.querySelector(".amount input");
let amtval= amount.value;
if(amtval==="" || amtval <0)
{ amtval=1;
amount.value= "1";
}
// const url = `https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@latest/v1/currencies/${fromcurr}.json`;
if(amtval.length!=0){
fetch(api)
.then((resp)=>resp.json())
.then((data)=>{
let fromExgRate= data.conversion_rates[fromcurr.value];
let toExgRate= data.conversion_rates[tocurr.value];
let covertedamt= (amtval/fromExgRate)*toExgRate;
msg.innerText = `${amtval} ${fromcurr.value} = ${covertedamt.toFixed(2)} ${tocurr.value}`;
// console.log(covertedamt.toFixed(2));
})
}
// const URL= base_url+'/'+fromcurr.value.toLowerCase()+'.json';
// let response= await fetch(URL);
// let data = await response.json();
// let rate= data[tocurr.value.toLowerCase()];
// let finalamount= amtval*rate;
// console.log(rate);
// console.log(amtval);
// msg.innerText= amtval+' ' + fromcurr.value + '=' + finalamount + ' '+ tocurr.value ;
}
const updateflag=(element)=>{
let currcode= element.value;
let countryCode= countryList[currcode];
let newsrc= "https://flagsapi.com/"+countryCode+"/flat/64.png" ;
let img= element.parentElement.querySelector("img");
img.src= newsrc;
};
btn.addEventListener("click", (evt)=>{
evt.preventDefault();
updateExchangeRate();
})
window.addEventListener('load',()=>{
updateExchangeRate();
});