-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
70 lines (62 loc) · 2.04 KB
/
script.js
File metadata and controls
70 lines (62 loc) · 2.04 KB
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
const BASE_URL =
"https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@latest/v1/currencies";
const dropdownSel = document.querySelectorAll(".dropdown select");
const convertBtn = document.querySelector("form button");
let fromCurr = document.querySelector(".From select");
let toCurr = document.querySelector(".To select");
let changeI = document.querySelector("i");
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 === "INR") {
newOption.selected = "selected";
} else if (select.name === "To" && currCode === "USD") {
newOption.selected = "selected";
}
select.append(newOption);
}
select.addEventListener("change", (evt) => {
updateFlag(evt.target);
});
}
async function updateExcgRate() {
let amount = document.querySelector(".amount input");
let amtVal = amount.value;
if (amtVal === "" || amtVal < 1) {
amtVal = 1;
amount.value = "1";
}
const URL = `${BASE_URL}/${fromCurr.value.toLowerCase()}.json`;
let response = await fetch(URL);
let data = await response.json();
let rate = data[fromCurr.value.toLowerCase()][toCurr.value.toLowerCase()];
let finalAmt = amtVal * rate;
msg.innerText = `${amtVal} ${fromCurr.value} ≈ ${finalAmt.toFixed(4)} ${
toCurr.value
}`;
}
window.addEventListener("load", () => {
updateExcgRate();
});
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;
};
convertBtn.addEventListener("click", (evt) => {
evt.preventDefault();
updateExcgRate();
});
changeI.addEventListener("click", () => {
let temp = fromCurr.value;
fromCurr.value = toCurr.value;
toCurr.value = temp;
updateFlag(fromCurr);
updateFlag(toCurr);
updateExcgRate();
});