Update script.js
Browse files
script.js
CHANGED
|
@@ -1,61 +1,77 @@
|
|
| 1 |
-
|
| 2 |
-
document.getElementById('
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
const toCurrency = document.getElementById('toCurrency').value;
|
| 8 |
-
const resultDiv = document.getElementById('result');
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
await convertCurrency(amount, fromCurrency, toCurrency, resultDiv);
|
| 17 |
});
|
| 18 |
|
| 19 |
-
//
|
| 20 |
-
|
| 21 |
-
const
|
| 22 |
-
const toCurrency = document.getElementById('toCurrency');
|
| 23 |
-
const resultDiv = document.getElementById('result');
|
| 24 |
-
|
| 25 |
-
// Tukar mata uang
|
| 26 |
-
const tempCurrency = fromCurrency.value;
|
| 27 |
fromCurrency.value = toCurrency.value;
|
| 28 |
-
toCurrency.value =
|
| 29 |
-
|
| 30 |
-
// Ambil nilai amount
|
| 31 |
-
const amount = document.getElementById('amount').value;
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
|
|
|
| 35 |
} else {
|
| 36 |
-
|
|
|
|
| 37 |
}
|
| 38 |
});
|
| 39 |
|
| 40 |
-
//
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
try {
|
| 43 |
const apiKey = '3ebe2ccf9eeea2aaef280201';
|
| 44 |
-
const url = `https://v6.exchangerate-api.com/v6/${apiKey}/latest/${
|
| 45 |
-
|
| 46 |
-
// Fetch data dari API
|
| 47 |
const response = await fetch(url);
|
| 48 |
const data = await response.json();
|
| 49 |
|
| 50 |
if (data.result === 'success') {
|
| 51 |
-
const rate = data.conversion_rates[
|
| 52 |
const convertedAmount = (amount * rate).toFixed(2);
|
| 53 |
-
resultDiv.innerHTML = `${amount} ${
|
| 54 |
} else {
|
| 55 |
resultDiv.innerHTML = `Error: ${data['error-type']}`;
|
| 56 |
}
|
| 57 |
} catch (error) {
|
| 58 |
-
resultDiv.innerHTML =
|
| 59 |
-
console.error(
|
| 60 |
}
|
| 61 |
}
|
|
|
|
| 1 |
+
const amountInput = document.getElementById('amount');
|
| 2 |
+
const fromCurrency = document.getElementById('fromCurrency');
|
| 3 |
+
const toCurrency = document.getElementById('toCurrency');
|
| 4 |
+
const swapButton = document.getElementById('swapButton');
|
| 5 |
+
const convertButton = document.getElementById('convertButton');
|
| 6 |
+
const resultDiv = document.getElementById('result');
|
| 7 |
|
| 8 |
+
// Status konversi otomatis
|
| 9 |
+
let autoConvert = false;
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
// Tombol convert pertama kali
|
| 12 |
+
convertButton.addEventListener('click', () => {
|
| 13 |
+
autoConvert = true; // Aktifkan konversi otomatis
|
| 14 |
+
convertCurrency(); // Lakukan konversi pertama kali
|
| 15 |
+
convertButton.style.display = 'none'; // Sembunyikan tombol
|
|
|
|
|
|
|
| 16 |
});
|
| 17 |
|
| 18 |
+
// Swap mata uang
|
| 19 |
+
swapButton.addEventListener('click', async () => {
|
| 20 |
+
const temp = fromCurrency.value;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
fromCurrency.value = toCurrency.value;
|
| 22 |
+
toCurrency.value = temp;
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
// Jika autoConvert aktif, langsung lakukan konversi
|
| 25 |
+
if (autoConvert) {
|
| 26 |
+
convertCurrency();
|
| 27 |
} else {
|
| 28 |
+
// Jika belum autoConvert, kosongkan hasil
|
| 29 |
+
resultDiv.innerHTML = '';
|
| 30 |
}
|
| 31 |
});
|
| 32 |
|
| 33 |
+
// Pantau perubahan input atau dropdown
|
| 34 |
+
[amountInput, fromCurrency, toCurrency].forEach((element) => {
|
| 35 |
+
element.addEventListener('input', () => {
|
| 36 |
+
if (autoConvert) {
|
| 37 |
+
convertCurrency(); // Konversi otomatis saat autoConvert aktif
|
| 38 |
+
}
|
| 39 |
+
});
|
| 40 |
+
|
| 41 |
+
element.addEventListener('change', () => {
|
| 42 |
+
// Reset autoConvert, tampilkan tombol Convert kembali
|
| 43 |
+
autoConvert = false;
|
| 44 |
+
convertButton.style.display = 'block';
|
| 45 |
+
resultDiv.innerHTML = '';
|
| 46 |
+
});
|
| 47 |
+
});
|
| 48 |
+
|
| 49 |
+
// Fungsi konversi
|
| 50 |
+
async function convertCurrency() {
|
| 51 |
+
const amount = amountInput.value;
|
| 52 |
+
const from = fromCurrency.value;
|
| 53 |
+
const to = toCurrency.value;
|
| 54 |
+
|
| 55 |
+
if (!amount || amount <= 0) {
|
| 56 |
+
resultDiv.innerHTML = 'Please enter a valid amount.';
|
| 57 |
+
return;
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
try {
|
| 61 |
const apiKey = '3ebe2ccf9eeea2aaef280201';
|
| 62 |
+
const url = `https://v6.exchangerate-api.com/v6/${apiKey}/latest/${from}`;
|
|
|
|
|
|
|
| 63 |
const response = await fetch(url);
|
| 64 |
const data = await response.json();
|
| 65 |
|
| 66 |
if (data.result === 'success') {
|
| 67 |
+
const rate = data.conversion_rates[to];
|
| 68 |
const convertedAmount = (amount * rate).toFixed(2);
|
| 69 |
+
resultDiv.innerHTML = `${amount} ${from} = ${convertedAmount} ${to}`;
|
| 70 |
} else {
|
| 71 |
resultDiv.innerHTML = `Error: ${data['error-type']}`;
|
| 72 |
}
|
| 73 |
} catch (error) {
|
| 74 |
+
resultDiv.innerHTML = 'Error fetching conversion rate.';
|
| 75 |
+
console.error(error);
|
| 76 |
}
|
| 77 |
}
|