-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
64 lines (54 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
61
62
63
64
let BaseURL = "https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/latest/currencies";
let dropdown = document.querySelectorAll(".all select");
let btn = document.querySelector("button");
let from = document.querySelector("#fr");
let to = document.querySelector("#too");
let msg = document.querySelector("#msg");
for (let select of dropdown){
for (currcode in countryList) {
let newoption = document.createElement("option");
newoption.value = currcode;
newoption.innerText = 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", (e) => {
updateflag(e.target);
})
}
const exchangerate = () => {
let amount = document.querySelector("input");
if(amount.value===" " || amount.value < 1){
msg.innerText = "Enter a Valid Amount";
return;
}
let fromcurrency = from.value;
let tocurrency = to.value;
if (fromcurrency === tocurrency) {
msg.innerText = "Choose Different Currency";
} else {
let url = `${BaseURL}/${fromcurrency.toLowerCase()}/${tocurrency.toLowerCase()}.json`;
fetch(url)
.then(response => response.json())
.then(data => {
let rate = data[tocurrency.toLowerCase()];
let result = rate * amount.value;
msg.innerText = `${amount.value} ${fromcurrency} = ${result.toFixed(2)} ${tocurrency}`;
})
}
}
const updateflag = (element) => {
let currcode = element.value;
let countrycode = countryList[currcode];
let flagurl = `https://flagsapi.com/${countrycode}/flat/64.png`;
let img = element.parentElement.querySelector("img");
img.src = flagurl;
}
btn.addEventListener("click", (evt) => {
evt.preventDefault();
exchangerate();
});