-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
39 lines (34 loc) · 1.2 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
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');
// Fetching exchange rates from API and updating the DOM
function calculate() {
const currOne = currencyOne.value;
const currTwo = currencyTwo.value;
// Using Fetch
fetch(
`https://v6.exchangerate-api.com/v6/e3a9431a6df186387eec4a21/latest/${currOne}`
)
.then((res) => res.json())
.then((data) => {
// console.log(data);
const ratee = data.conversion_rates[currTwo];
rate.innerText = `1 ${currOne} = ${ratee} ${currTwo}`;
amountTwo.value = (amountOne.value * ratee).toFixed(2);
});
}
//Evenet listeners
currencyOne.addEventListener('change', calculate);
amountOne.addEventListener('input', calculate);
currencyTwo.addEventListener('change', calculate);
amountTwo.addEventListener('input', calculate);
swap.addEventListener('click', () => {
const tmp = currencyOne.value;
currencyOne.value = currencyTwo.value;
currencyTwo.value = tmp;
calculate();
});
calculate();