CurrencyConverter5 / script.js
GarGerry's picture
Update script.js
13daff1 verified
raw
history blame
1.23 kB
// Fungsi untuk melakukan konversi mata uang
async function convertCurrency() {
const amount = document.getElementById('amount').value;
const fromCurrency = document.getElementById('from-currency').value;
const toCurrency = document.getElementById('to-currency').value;
const result = document.getElementById('result');
const convertBtn = document.getElementById('convert-btn');
if (amount && fromCurrency && toCurrency) {
try {
// Gunakan API dengan API key yang valid
const response = await fetch(`https://v6.exchangerate-api.com/v6/3ebe2ccf9eeea2aaef280201/latest/${fromCurrency}`);
const data = await response.json();
const rate = data.rates[toCurrency];
const convertedAmount = (amount * rate).toFixed(2);
// Menampilkan hasil konversi
result.innerHTML = `${amount} ${fromCurrency} = ${convertedAmount} ${toCurrency}`;
// Sembunyikan tombol Convert setelah konversi otomatis
convertBtn.style.display = 'none';
} catch (error) {
result.innerText = "Error fetching exchange rates!";
console.error('Error fetching exchange rates:', error);
}
}
}