-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathscript.js
32 lines (28 loc) · 1.13 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
const currencyOne = document.getElementById("currency-one");
const amountOne = document.getElementById("amount-one");
const currencyTwo = document.getElementById("currency-two");
const amountTwo = document.getElementById("amount-two");
const rate = document.getElementById("rate");
const swap = document.getElementById("swap");
function calculate() {
const currency_one = currencyOne.value;
const currency_two = currencyTwo.value;
fetch(`https://api.exchangerate-api.com/v4/latest/${currency_one}`)
.then((res) => res.json())
.then((data) => {
const currentRate = data.rates[currency_two];
rate.innerText = `1 ${currency_one} = ${currentRate} ${currency_two}`;
amountTwo.value = (amountOne.value * currentRate).toFixed(2);
});
}
currencyOne.addEventListener("change", calculate);
amountOne.addEventListener("input", calculate);
currencyTwo.addEventListener("change", calculate);
amountTwo.addEventListener("input", calculate);
swap.addEventListener("click", () => {
const storedValue = currencyOne.value;
currencyOne.value = currencyTwo.value;
currencyTwo.value = storedValue;
calculate();
});
calculate();