-
Notifications
You must be signed in to change notification settings - Fork 0
/
script1.js
36 lines (30 loc) · 1.24 KB
/
script1.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
//https://api.exchangerate-api.com/v4/latest/USD
const currencyEl_one = document.getElementById('currency-one');
const amountEl_one = document.getElementById('amount-one');
const currencyEl_two = document.getElementById('currency-two');
const amountEl_two = document.getElementById('amount-two');
const rateEl = document.getElementById('rate');
const swap = document.getElementById('swap');
// Fetch Exchange rate and Calculate
function calculate(){
const currencyOne = currencyEl_one.value;
const currencyTwo = currencyEl_two.value;
fetch(`https://api.exchangerate-api.com/v4/latest/${currencyOne}`)
.then( res => res.json())
.then( data => {
const rate = data.rates[currencyTwo];
amountEl_two.value = (rate * amountEl_one.value).toFixed(2);
rateEl.innerText = `1 ${currencyOne} = ${rate} ${currencyTwo}`;
});
}
// Event Listeners
currencyEl_one.addEventListener('change', calculate);
amountEl_one.addEventListener('input', calculate);
currencyEl_two.addEventListener('change', calculate);
swap.addEventListener('click', () => {
const temp = currencyEl_one.value;
currencyEl_one.value = currencyEl_two.value;
currencyEl_two.value = temp;
calculate();
});
calculate();