-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
49 lines (29 loc) · 1.31 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
//https://app.exchangerate-api.com
const currencySelectOne = document.querySelector('#select-currency-one');
const amountInputOne = document.querySelector('#amount-one-input');
const currencySelectTwo = document.querySelector('#select-currency-two');
const amountInputTwo = document.querySelector('#amount-two-input');
const rate = document.querySelector('#div-message-rate');
const btnSwap = document.querySelector('#btn-swap');
currencySelectOne.addEventListener('change', calculateRate);
currencySelectTwo.addEventListener('change', calculateRate);
amountInputOne.addEventListener('input', calculateRate);
amountInputTwo.addEventListener('input', calculateRate);
btnSwap.addEventListener('click', function(){
const tempValue = currencySelectOne.value;
currencySelectOne.value = currencySelectTwo.value;
currencySelectTwo.value = tempValue;
calculateRate();
});
function calculateRate(){
const currencyOne = currencySelectOne.value;
const currencyTwo = currencySelectTwo.value;
fetch(`https://open.er-api.com/v6/latest/${currencyOne}`)
.then((res)=> res.json())
.then((data) => {
const rateData = data.rates[currencyTwo];
rate.innerHTML = ` 1 ${currencyOne} = ${rateData} ${currencyTwo}`;
amountInputTwo.value = (amountInputOne.value * rateData).toFixed(2);
});
}
calculateRate();