|
1 |
| -const exchange_rate = document.getElementById('exchange-rate'); |
2 |
| -const curr_first = document.getElementById('curr-first'); |
3 |
| -const curr_second = document.getElementById('curr-second'); |
4 |
| -const worth_first = document.getElementById('worth-first'); |
5 |
| -const worth_second = document.getElementById('worth-second'); |
| 1 | +const exchangeRateEl = document.getElementById("exchange-rate"); |
| 2 | +const currencyFirstEl = document.getElementById("currency-first"); |
| 3 | +const currencySecondEl = document.getElementById("currency-second"); |
| 4 | +const worthFirstEl = document.getElementById("worth-first"); |
| 5 | +const worthSecondEl = document.getElementById("worth-second"); |
6 | 6 |
|
7 | 7 | function convert() {
|
8 |
| - const currency_first = curr_first.value; |
9 |
| - const currency_second = curr_second.value; |
10 |
| -//using API for conversion of currency units |
11 |
| - fetch(`https://v6.exchangerate-api.com/v6/16947c81da979880bacde4f5/latest/${currency_first}`) |
| 8 | + const currencyFirstValue = currencyFirstEl.value; |
| 9 | + const currencySecondValue = currencySecondEl.value; |
| 10 | + //using API for conversion of currency units |
| 11 | + fetch( |
| 12 | + `https://v6.exchangerate-api.com/v6/5f9d1c87f7250159c9c9b17d/latest/${currencyFirstValue}` |
| 13 | + ) |
12 | 14 | .then((res) => res.json())
|
13 | 15 | .then((data) => {
|
14 |
| - |
15 |
| - const rate = data.conversion_rates[currency_second]; |
16 |
| - exchange_rate.innerText = `1 ${currency_first} = ${rate} ${currency_second}`; |
| 16 | + const rate = data.conversion_rates[currencySecondValue]; |
| 17 | + exchangeRateEl.innerText = `1 ${currencyFirstValue} = ${rate} ${currencySecondValue}`; |
17 | 18 |
|
18 |
| - worth_second.value = (worth_first.value * rate).toFixed(5); |
| 19 | + worthSecondEl.value = (worthFirstEl.value * rate).toFixed(2); |
19 | 20 | });
|
20 | 21 | }
|
21 | 22 | //some javascript event listeners
|
22 |
| -curr_first.addEventListener('change', convert); |
23 |
| -worth_first.addEventListener('input', convert); |
24 |
| -curr_second.addEventListener('change', convert); |
25 |
| -worth_second.addEventListener('input', convert); |
| 23 | +currencyFirstEl.addEventListener("change", convert); |
| 24 | +worthFirstEl.addEventListener("input", convert); |
| 25 | +currencySecondEl.addEventListener("change", convert); |
| 26 | +worthSecondEl.addEventListener("input", convert); |
26 | 27 | convert();
|
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 |
| - |
0 commit comments