-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
63 lines (56 loc) · 1.99 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
const dropdowns=document.querySelectorAll(".dropdown select");
let btn=document.querySelector("form button");
const fromCurr=document.querySelector(".from select");
const toCurr=document.querySelector(".to select");
let msg=document.querySelector(".msg");
for(let select of dropdowns){
for(currCode in countryList){
let newOption=document.createElement("option");
newOption.innerText=currCode;
newOption.value=currCode;
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",(event)=>{
updateFlag(event.target);
});
}
const updateExchangeRate=async ()=>{
let amount=document.querySelector(".amount input");
let amountVal=amount.value;
if(amountVal==="" || amountVal<1){
amountVal=1;
amount.value="1";
}
let fromCurrency = fromCurr.value;
let toCurrency = toCurr.value;
let url = `https://v6.exchangerate-api.com/v6/14a7a7491309995f7463dbb2/latest/${fromCurrency}`;
try {
let res = await fetch(url);
let data = await res.json();
let rate = data.conversion_rates[toCurrency];
let finalAmt = amountVal * rate;
let finalAmtr=finalAmt.toFixed(2);
msg.innerText=`${amountVal} ${fromCurr.value} = ${finalAmtr} ${toCurr.value}`;
} catch (err) {
console.log("Error:", err);
}
};
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", (event)=>{
event.preventDefault();
updateExchangeRate();
});
window.addEventListener("load",()=>{
updateExchangeRate();
});