File size: 1,234 Bytes
ddc13b5
6e0fcb6
352b6a4
 
 
 
 
 
 
 
13daff1
 
352b6a4
 
 
 
ddc13b5
352b6a4
 
ddc13b5
352b6a4
 
 
13daff1
352b6a4
c1aba63
13daff1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// 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);
        }
    }
}