File size: 4,173 Bytes
ddc13b5
6e0fcb6
352b6a4
 
 
 
 
 
37ca035
352b6a4
 
37ca035
352b6a4
 
 
 
 
ddc13b5
352b6a4
 
ddc13b5
352b6a4
 
 
 
c1aba63
6e0fcb6
 
ddc13b5
6e0fcb6
352b6a4
 
6e0fcb6
ddc13b5
352b6a4
 
 
6e0fcb6
ddc13b5
352b6a4
6e0fcb6
 
ddc13b5
352b6a4
ddc13b5
 
6e0fcb6
352b6a4
 
6e0fcb6
 
 
352b6a4
 
fd49a35
37ca035
 
fd49a35
 
37ca035
fd49a35
37ca035
fd49a35
 
 
37ca035
 
 
 
fd49a35
 
 
37ca035
 
 
 
fd49a35
 
37ca035
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// 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');

    // Validasi input
    if (amount && fromCurrency && toCurrency) {
        try {
            // Mendapatkan data kurs mata uang
            const response = await fetch(`https://api.exchangerate-api.com/v4/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!";
        }
    }
}

// Fungsi untuk menukar mata uang From dan To
function swapCurrencies() {
    const fromCurrency = document.getElementById('from-currency');
    const toCurrency = document.getElementById('to-currency');

    // Tukar nilai mata uang
    const temp = fromCurrency.value;
    fromCurrency.value = toCurrency.value;
    toCurrency.value = temp;

    // Lakukan konversi otomatis setelah swap
    convertCurrency();
}

// Event listener untuk input amount agar otomatis konversi
document.getElementById('amount').addEventListener('input', convertCurrency);

// Event listener untuk perubahan mata uang (From dan To) agar tombol Convert muncul kembali
document.getElementById('from-currency').addEventListener('change', () => {
    document.getElementById('convert-btn').style.display = 'block';
    document.getElementById('result').innerHTML = '';
});

document.getElementById('to-currency').addEventListener('change', () => {
    document.getElementById('convert-btn').style.display = 'block';
    document.getElementById('result').innerHTML = '';
});

// Fungsi untuk menangani scroll dan mengganti warna latar belakang
window.onscroll = function() {
    const navbar = document.querySelector('.navbar');
    const howToUse = document.querySelector('.how-to-use');
    const steps = document.querySelector('.steps');
    const footer = document.querySelector('footer');

    if (window.scrollY > 200) {
        navbar.style.backgroundColor = '#004d00'; // Darker Green
        howToUse.style.backgroundColor = '#004d00'; // Darker Green
        steps.forEach(step => {
            step.style.backgroundColor = '#004d00'; // Darker Green for steps
        });
        footer.style.backgroundColor = '#666666'; // Darker Gray
    } else {
        navbar.style.backgroundColor = '#006400'; // Original Green
        howToUse.style.backgroundColor = '#006400'; // Original Green
        steps.forEach(step => {
            step.style.backgroundColor = '#006400'; // Original Green for steps
        });
        footer.style.backgroundColor = '#808080'; // Original Gray
    }
};

// Fungsi untuk memuat pilihan mata uang
function loadCurrencies() {
    const currencies = [
        'BND', 'KHR', 'IDR', 'LAK', 'MYR', 'MMK', 'PHP', 'SGD', 'THB', 'VND', 
        'EUR', 'GBP', 'CHF', 'RUB', 'TRY', 'USD', 'CAD', 'BRL', 'ARS', 'ZAR', 
        'JPY', 'CNY', 'KRW', 'KPW', 'HKD'
    ];
    
    const fromCurrency = document.getElementById('from-currency');
    const toCurrency = document.getElementById('to-currency');
    
    currencies.forEach(currency => {
        const optionFrom = document.createElement('option');
        optionFrom.value = currency;
        optionFrom.textContent = `${currency}`;
        fromCurrency.appendChild(optionFrom);

        const optionTo = document.createElement('option');
        optionTo.value = currency;
        optionTo.textContent = `${currency}`;
        toCurrency.appendChild(optionTo);
    });
}

// Memuat mata uang ketika halaman dimuat
window.onload = loadCurrencies;