-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
57 lines (38 loc) · 1.67 KB
/
app.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
const currencyEl_one = document.getElementById('currency-one');
const currencyEl_two = document.getElementById('currency-two');
const amountEl_one = document.getElementById('amount-one');
const amountEl_two = document.getElementById('amount-two');
const link = document.getElementById('currency');
const elements = document.getElementsByClassName("currency_body");
const rateEl = document.getElementById('rate');
const swap = document.getElementById('swap');
link.addEventListener("click", function(event) {
event.preventDefault();
for (let i = 0; i < elements.length; i++) {
elements[i].classList.toggle('show');
elements[i].style.display = "block";
}
});
function calculate(){
const currency_one = currencyEl_one.value
const currency_two = currencyEl_two.value
fetch(`https://v6.exchangerate-api.com/v6/492ec17ebb60eacf7ff577aa/latest/${currency_one}`)
.then((res) => res.json())
.then((data) => {
// console.log(data);
const rate = data.conversion_rates[currency_two];
rateEl.innerText=`1 ${currency_one} =${rate} ${currency_two}`;
amountEl_two.value = (amountEl_one.value * rate).toFixed(2);
swap.addEventListener('click' , () =>{
const temo = currencyEl_one.value;
currencyEl_two.value = currencyEl_one.value;
currencyEl_two.value=temp;
calculate();
})
});
}
currencyEl_one.addEventListener('change', calculate);
currencyEl_two.addEventListener('change', calculate);
amountEl_one.addEventListener('input', calculate);
amountEl_two.addEventListener('input', calculate);
calculate();