Skip to content

Commit 266dca8

Browse files
update currency converter project
1 parent c6c243a commit 266dca8

File tree

2 files changed

+20
-44
lines changed

2 files changed

+20
-44
lines changed

projects/currency-converter/index.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<h1>Currency Converter</h1>
1313

1414
<div class="currency">
15-
<select id="curr-first">
15+
<select id="currency-first">
1616
<option value="AUD">AUD</option>
1717
<option value="CAD">CAD</option>
1818
<option value="EUR">EUR</option>
@@ -27,7 +27,7 @@ <h1>Currency Converter</h1>
2727

2828

2929
<div class="currency">
30-
<select id="curr-second">
30+
<select id="currency-second">
3131
<option value="AUD">AUD</option>
3232
<option value="CAD">CAD</option>
3333
<option value="EUR">EUR</option>

projects/currency-converter/index.js

+18-42
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,27 @@
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");
66

77
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+
)
1214
.then((res) => res.json())
1315
.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}`;
1718

18-
worth_second.value = (worth_first.value * rate).toFixed(5);
19+
worthSecondEl.value = (worthFirstEl.value * rate).toFixed(2);
1920
});
2021
}
2122
//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);
2627
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

Comments
 (0)