-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
53 lines (49 loc) · 1.73 KB
/
script.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
const baseURL="https://latest.currency-api.pages.dev/v1/currencies";
const dropDown=document.querySelectorAll(".selector");
const btn=document.querySelector("form button");
const fromCurr=document.querySelector("#fc");
const toCurr=document.querySelector("#tc");
const msg=document.querySelector(".msg");
for(let select of dropDown){
for(currCode in countryList){
let newOption=document.createElement("option");
newOption.innerText=currCode;
newOption.value=currCode;
if(select.name==="fromCurrency"&&currCode==="USD"){
newOption.selected="selected"
}
else if(select.name==="toCurrency"&&currCode==="INR"){
newOption.selected="selected"
}
select.append(newOption);
}
select.addEventListener("change", (evt)=>{
updateFlag(evt.target);
})
}
const updateFlag=(elem)=>{
let currCode=elem.value;
let countryCode=countryList[currCode];
let newSrc=`https://flagsapi.com/${countryCode}/flat/64.png`;
let img=elem.parentElement.querySelector("img");
img.src=newSrc;
}
btn.addEventListener("click",async (evt)=>{
evt.preventDefault();
let amount=document.querySelector("#amount");
if(amount.value===""||amount.value<0){
amount.value="1";
}
const URL=`${baseURL}/${fromCurr.value.toLowerCase()}.json`;
let response=await fetch(URL);
let data=await response.json();
let fromData=data[fromCurr.value.toLowerCase()];
let toData=fromData[toCurr.value.toLowerCase()];
let rate=toData;
let finalAmount=rate*(amount.value);
if(isNaN(finalAmount)){
msg.innerText=`Invalid Input`;
}else{
msg.innerText=`${amount.value} ${fromCurr.value}=${finalAmount} ${toCurr.value}`;
}
});