-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
50 lines (43 loc) · 1.56 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
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);
// Adding animation to the rate display
rate.classList.add("fade");
setTimeout(() => rate.classList.remove("fade"), 500);
});
}
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();
});
// Adding CSS class for fade effect
const style = document.createElement("style");
style.innerHTML = `
.fade {
animation: fadeIn 0.5s ease-in-out;
}
@keyframes fadeIn {
0% { opacity: 0; }
100% { opacity: 1; }
}
`;
document.head.appendChild(style);
calculate();