Update script.js
Browse files
script.js
CHANGED
|
@@ -1,74 +1,61 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
const swapButton = document.getElementById('swapButton');
|
| 5 |
-
const convertButton = document.querySelector('.convert-button'); // Disesuaikan dengan kelas tombol
|
| 6 |
-
const resultDiv = document.getElementById('result');
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
//
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
});
|
| 17 |
-
|
| 18 |
-
// Swap mata uang
|
| 19 |
-
swapButton.addEventListener('click', () => {
|
| 20 |
-
const temp = fromCurrency.value;
|
| 21 |
-
fromCurrency.value = toCurrency.value;
|
| 22 |
-
toCurrency.value = temp;
|
| 23 |
|
| 24 |
-
|
| 25 |
-
autoConvert = false;
|
| 26 |
-
convertButton.style.display = 'block';
|
| 27 |
-
resultDiv.innerHTML = '';
|
| 28 |
});
|
| 29 |
|
| 30 |
-
//
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
}
|
| 36 |
-
});
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
resultDiv.innerHTML = '';
|
| 43 |
-
});
|
| 44 |
-
});
|
| 45 |
|
| 46 |
-
//
|
| 47 |
-
|
| 48 |
-
const amount = amountInput.value;
|
| 49 |
-
const from = fromCurrency.value;
|
| 50 |
-
const to = toCurrency.value;
|
| 51 |
|
| 52 |
-
if (
|
| 53 |
-
|
| 54 |
-
|
|
|
|
| 55 |
}
|
|
|
|
| 56 |
|
|
|
|
|
|
|
| 57 |
try {
|
| 58 |
const apiKey = '3ebe2ccf9eeea2aaef280201';
|
| 59 |
-
const url = `https://v6.exchangerate-api.com/v6/${apiKey}/latest/${
|
|
|
|
|
|
|
| 60 |
const response = await fetch(url);
|
| 61 |
const data = await response.json();
|
| 62 |
|
| 63 |
if (data.result === 'success') {
|
| 64 |
-
const rate = data.conversion_rates[
|
| 65 |
const convertedAmount = (amount * rate).toFixed(2);
|
| 66 |
-
resultDiv.innerHTML = `${amount} ${
|
| 67 |
} else {
|
| 68 |
resultDiv.innerHTML = `Error: ${data['error-type']}`;
|
| 69 |
}
|
| 70 |
} catch (error) {
|
| 71 |
-
resultDiv.innerHTML =
|
| 72 |
-
console.error(error);
|
| 73 |
}
|
| 74 |
}
|
|
|
|
| 1 |
+
// Fungsi untuk menangani submit form
|
| 2 |
+
document.getElementById('currencyForm').addEventListener('submit', async function (event) {
|
| 3 |
+
event.preventDefault();
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
const amount = document.getElementById('amount').value;
|
| 6 |
+
const fromCurrency = document.getElementById('fromCurrency').value;
|
| 7 |
+
const toCurrency = document.getElementById('toCurrency').value;
|
| 8 |
+
const resultDiv = document.getElementById('result');
|
| 9 |
|
| 10 |
+
// Validasi input amount
|
| 11 |
+
if (!amount || amount <= 0) {
|
| 12 |
+
resultDiv.innerHTML = "Please enter a valid amount!";
|
| 13 |
+
return;
|
| 14 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
await convertCurrency(amount, fromCurrency, toCurrency, resultDiv);
|
|
|
|
|
|
|
|
|
|
| 17 |
});
|
| 18 |
|
| 19 |
+
// Fungsi untuk menangani tombol swap
|
| 20 |
+
document.getElementById('swapButton').addEventListener('click', async function () {
|
| 21 |
+
const fromCurrency = document.getElementById('fromCurrency');
|
| 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 = tempCurrency;
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
// Ambil nilai amount
|
| 31 |
+
const amount = document.getElementById('amount').value;
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
if (amount && amount > 0) {
|
| 34 |
+
await convertCurrency(amount, fromCurrency.value, toCurrency.value, resultDiv);
|
| 35 |
+
} else {
|
| 36 |
+
resultDiv.innerHTML = "";
|
| 37 |
}
|
| 38 |
+
});
|
| 39 |
|
| 40 |
+
// Fungsi konversi mata uang
|
| 41 |
+
async function convertCurrency(amount, fromCurrency, toCurrency, resultDiv) {
|
| 42 |
try {
|
| 43 |
const apiKey = '3ebe2ccf9eeea2aaef280201';
|
| 44 |
+
const url = `https://v6.exchangerate-api.com/v6/${apiKey}/latest/${fromCurrency}`;
|
| 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[toCurrency];
|
| 52 |
const convertedAmount = (amount * rate).toFixed(2);
|
| 53 |
+
resultDiv.innerHTML = `${amount} ${fromCurrency} = ${convertedAmount} ${toCurrency}`;
|
| 54 |
} else {
|
| 55 |
resultDiv.innerHTML = `Error: ${data['error-type']}`;
|
| 56 |
}
|
| 57 |
} catch (error) {
|
| 58 |
+
resultDiv.innerHTML = "Error fetching conversion rate.";
|
| 59 |
+
console.error("Error:", error);
|
| 60 |
}
|
| 61 |
}
|