-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
60 lines (51 loc) · 1.96 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
53
54
55
56
57
58
59
60
const baseURL = "https://api.frankfurter.app/latest?amount=10&from=GBP&to=USD";
const dropdownsel = 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 dropdownsel){
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",(evt)=>{
updateFlag(evt.target);
});
}
const updateExchangeRate = async () => {
let amount = document.querySelector(".amount input");
let amtvalue = amount.value;
if(amtvalue === "" || amtvalue <1){
amtvalue = 1;
amount.value =1;
}
const url = `https://api.frankfurter.app/latest?amount=10&from=${fromCurr.value}&to=${toCurr.value}`;
let response = await fetch(url);
let data = await response.json();
let exchangeRate = (data.rates[toCurr.value])/10; //json file mai 10amt ka dikha rha tha
let finalAmt = exchangeRate*amtvalue;
msg.innerText = `${amtvalue} ${fromCurr.value} = ${finalAmt} ${toCurr.value}`;
};
const updateFlag = (element) =>{ //gives target
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();
});