-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
53 lines (46 loc) · 1.66 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
baseURL = "https://v6.exchangerate-api.com/v6/7ad5476b65bc6dcf90111490/latest"
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(currcode in countryList){
let newOption = document.createElement("option");
newOption.innerText = currcode;
newOption.value = currcode;
if(select.name === "from" && currcode === "USD"){
newOption.selected = "selected"
}
if(select.name === "to" && currcode === "INR"){
newOption.selected = "selected"
}
select.append(newOption)
}
select.addEventListener("change", (evt) => {
updateFlag(evt.target);
});
}
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", async (evt) => {
evt.preventDefault();
let amt = document.querySelector(".amount input");
let amtVal = amt.value;
if(amtVal === "" || amtVal < 1){
amtVal = 1;
amt.value = "1";
}
const URL = `${baseURL}/${fromcurr.value}`
let reponse = await fetch(URL);
let data = await reponse.json();
let rate = data.conversion_rates[tocurr.value];
console.log(rate);
let finalAmt = amt.value * rate;
msg.innerText = `${amtVal} ${fromcurr.value} = ${finalAmt} ${tocurr.value}`;
});