-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (37 loc) · 1.2 KB
/
index.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
const select = document.querySelectorAll(".currency");
const btn = document.getElementById("convertbtn");
const num = document.getElementById("num");
const ans = document.getElementById("ans");
fetch("https://api.frankfurter.app/currencies")
.then((data) => data.json())
.then((data) => {
display(data);
});
function display(data) {
const entries = Object.entries(data);
for (var i = 0; i < entries.length; i++) {
select[0].innerHTML += `<option value="${entries[i][0]}">${entries[i][0]}</option>`;
select[1].innerHTML += `<option value="${entries[i][0]}">${entries[i][0]}</option>`;
}
}
btn.addEventListener("click", () => {
let currency1 = select[0].value;
let currency2 = select[1].value;
let value = num.value;
if (currency1 != currency2) {
convert(currency1, currency2, value);
} else {
alert("The Currencies Should Not Be The Same");
}
});
function convert(currency1, currency2, value) {
const host = "api.frankfurter.app";
fetch(
`https://${host}/latest?amount=${value}&from=${currency1}&to=${currency2}`
)
.then((val) => val.json())
.then((val) => {
console.log(Object.values(val.rates)[0]);
ans.value = Object.values(val.rates)[0];
});
}