Spaces:
Running
Running
Update script.js
Browse files
script.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
//
|
| 2 |
async function convertCurrency() {
|
| 3 |
const amount = document.getElementById('amount').value;
|
| 4 |
const fromCurrency = document.getElementById('from-currency').value;
|
|
@@ -13,10 +13,10 @@ async function convertCurrency() {
|
|
| 13 |
const rate = data.rates[toCurrency];
|
| 14 |
const convertedAmount = (amount * rate).toFixed(2);
|
| 15 |
|
| 16 |
-
//
|
| 17 |
result.innerHTML = `${amount} ${fromCurrency} = ${convertedAmount} ${toCurrency}`;
|
| 18 |
|
| 19 |
-
//
|
| 20 |
convertBtn.style.display = 'none';
|
| 21 |
} catch (error) {
|
| 22 |
result.innerText = "Error fetching exchange rates!";
|
|
@@ -24,22 +24,24 @@ async function convertCurrency() {
|
|
| 24 |
}
|
| 25 |
}
|
| 26 |
|
| 27 |
-
//
|
| 28 |
function swapCurrencies() {
|
| 29 |
const fromCurrency = document.getElementById('from-currency');
|
| 30 |
const toCurrency = document.getElementById('to-currency');
|
| 31 |
|
| 32 |
-
//
|
| 33 |
const temp = fromCurrency.value;
|
| 34 |
fromCurrency.value = toCurrency.value;
|
| 35 |
toCurrency.value = temp;
|
| 36 |
|
| 37 |
-
//
|
| 38 |
convertCurrency();
|
| 39 |
}
|
| 40 |
|
| 41 |
-
// Event
|
| 42 |
document.getElementById('amount').addEventListener('input', convertCurrency);
|
|
|
|
|
|
|
| 43 |
document.getElementById('from-currency').addEventListener('change', () => {
|
| 44 |
document.getElementById('convert-btn').style.display = 'block';
|
| 45 |
document.getElementById('result').innerHTML = '';
|
|
|
|
| 1 |
+
// Fungsi untuk melakukan konversi mata uang
|
| 2 |
async function convertCurrency() {
|
| 3 |
const amount = document.getElementById('amount').value;
|
| 4 |
const fromCurrency = document.getElementById('from-currency').value;
|
|
|
|
| 13 |
const rate = data.rates[toCurrency];
|
| 14 |
const convertedAmount = (amount * rate).toFixed(2);
|
| 15 |
|
| 16 |
+
// Menampilkan hasil konversi
|
| 17 |
result.innerHTML = `${amount} ${fromCurrency} = ${convertedAmount} ${toCurrency}`;
|
| 18 |
|
| 19 |
+
// Sembunyikan tombol Convert setelah konversi otomatis
|
| 20 |
convertBtn.style.display = 'none';
|
| 21 |
} catch (error) {
|
| 22 |
result.innerText = "Error fetching exchange rates!";
|
|
|
|
| 24 |
}
|
| 25 |
}
|
| 26 |
|
| 27 |
+
// Fungsi untuk menukar mata uang From dan To
|
| 28 |
function swapCurrencies() {
|
| 29 |
const fromCurrency = document.getElementById('from-currency');
|
| 30 |
const toCurrency = document.getElementById('to-currency');
|
| 31 |
|
| 32 |
+
// Tukar nilai mata uang
|
| 33 |
const temp = fromCurrency.value;
|
| 34 |
fromCurrency.value = toCurrency.value;
|
| 35 |
toCurrency.value = temp;
|
| 36 |
|
| 37 |
+
// Lakukan konversi otomatis setelah swap
|
| 38 |
convertCurrency();
|
| 39 |
}
|
| 40 |
|
| 41 |
+
// Event listener untuk input amount agar otomatis konversi
|
| 42 |
document.getElementById('amount').addEventListener('input', convertCurrency);
|
| 43 |
+
|
| 44 |
+
// Event listener untuk perubahan mata uang (From dan To) agar tombol Convert muncul kembali
|
| 45 |
document.getElementById('from-currency').addEventListener('change', () => {
|
| 46 |
document.getElementById('convert-btn').style.display = 'block';
|
| 47 |
document.getElementById('result').innerHTML = '';
|