Update script.js
Browse files
script.js
CHANGED
@@ -6,11 +6,10 @@ async function convertCurrency() {
|
|
6 |
const result = document.getElementById('result');
|
7 |
const convertBtn = document.getElementById('convert-btn');
|
8 |
|
9 |
-
// Validasi input
|
10 |
if (amount && fromCurrency && toCurrency) {
|
11 |
try {
|
12 |
-
//
|
13 |
-
const response = await fetch(`https://
|
14 |
const data = await response.json();
|
15 |
const rate = data.rates[toCurrency];
|
16 |
const convertedAmount = (amount * rate).toFixed(2);
|
@@ -22,85 +21,7 @@ async function convertCurrency() {
|
|
22 |
convertBtn.style.display = 'none';
|
23 |
} catch (error) {
|
24 |
result.innerText = "Error fetching exchange rates!";
|
|
|
25 |
}
|
26 |
}
|
27 |
-
}
|
28 |
-
|
29 |
-
// Fungsi untuk menukar mata uang From dan To
|
30 |
-
function swapCurrencies() {
|
31 |
-
const fromCurrency = document.getElementById('from-currency');
|
32 |
-
const toCurrency = document.getElementById('to-currency');
|
33 |
-
|
34 |
-
// Tukar nilai mata uang
|
35 |
-
const temp = fromCurrency.value;
|
36 |
-
fromCurrency.value = toCurrency.value;
|
37 |
-
toCurrency.value = temp;
|
38 |
-
|
39 |
-
// Lakukan konversi otomatis setelah swap
|
40 |
-
convertCurrency();
|
41 |
-
}
|
42 |
-
|
43 |
-
// Event listener untuk input amount agar otomatis konversi
|
44 |
-
document.getElementById('amount').addEventListener('input', convertCurrency);
|
45 |
-
|
46 |
-
// Event listener untuk perubahan mata uang (From dan To) agar tombol Convert muncul kembali
|
47 |
-
document.getElementById('from-currency').addEventListener('change', () => {
|
48 |
-
document.getElementById('convert-btn').style.display = 'block';
|
49 |
-
document.getElementById('result').innerHTML = '';
|
50 |
-
});
|
51 |
-
|
52 |
-
document.getElementById('to-currency').addEventListener('change', () => {
|
53 |
-
document.getElementById('convert-btn').style.display = 'block';
|
54 |
-
document.getElementById('result').innerHTML = '';
|
55 |
-
});
|
56 |
-
|
57 |
-
// Fungsi untuk menangani scroll dan mengganti warna latar belakang
|
58 |
-
window.onscroll = function() {
|
59 |
-
const navbar = document.querySelector('.navbar');
|
60 |
-
const howToUse = document.querySelector('.how-to-use');
|
61 |
-
const steps = document.querySelector('.steps');
|
62 |
-
const footer = document.querySelector('footer');
|
63 |
-
|
64 |
-
if (window.scrollY > 200) {
|
65 |
-
navbar.style.backgroundColor = '#004d00'; // Darker Green
|
66 |
-
howToUse.style.backgroundColor = '#004d00'; // Darker Green
|
67 |
-
steps.forEach(step => {
|
68 |
-
step.style.backgroundColor = '#004d00'; // Darker Green for steps
|
69 |
-
});
|
70 |
-
footer.style.backgroundColor = '#666666'; // Darker Gray
|
71 |
-
} else {
|
72 |
-
navbar.style.backgroundColor = '#006400'; // Original Green
|
73 |
-
howToUse.style.backgroundColor = '#006400'; // Original Green
|
74 |
-
steps.forEach(step => {
|
75 |
-
step.style.backgroundColor = '#006400'; // Original Green for steps
|
76 |
-
});
|
77 |
-
footer.style.backgroundColor = '#808080'; // Original Gray
|
78 |
-
}
|
79 |
-
};
|
80 |
-
|
81 |
-
// Fungsi untuk memuat pilihan mata uang
|
82 |
-
function loadCurrencies() {
|
83 |
-
const currencies = [
|
84 |
-
'BND', 'KHR', 'IDR', 'LAK', 'MYR', 'MMK', 'PHP', 'SGD', 'THB', 'VND',
|
85 |
-
'EUR', 'GBP', 'CHF', 'RUB', 'TRY', 'USD', 'CAD', 'BRL', 'ARS', 'ZAR',
|
86 |
-
'JPY', 'CNY', 'KRW', 'KPW', 'HKD'
|
87 |
-
];
|
88 |
-
|
89 |
-
const fromCurrency = document.getElementById('from-currency');
|
90 |
-
const toCurrency = document.getElementById('to-currency');
|
91 |
-
|
92 |
-
currencies.forEach(currency => {
|
93 |
-
const optionFrom = document.createElement('option');
|
94 |
-
optionFrom.value = currency;
|
95 |
-
optionFrom.textContent = `${currency}`;
|
96 |
-
fromCurrency.appendChild(optionFrom);
|
97 |
-
|
98 |
-
const optionTo = document.createElement('option');
|
99 |
-
optionTo.value = currency;
|
100 |
-
optionTo.textContent = `${currency}`;
|
101 |
-
toCurrency.appendChild(optionTo);
|
102 |
-
});
|
103 |
-
}
|
104 |
-
|
105 |
-
// Memuat mata uang ketika halaman dimuat
|
106 |
-
window.onload = loadCurrencies;
|
|
|
6 |
const result = document.getElementById('result');
|
7 |
const convertBtn = document.getElementById('convert-btn');
|
8 |
|
|
|
9 |
if (amount && fromCurrency && toCurrency) {
|
10 |
try {
|
11 |
+
// Gunakan API dengan API key yang valid
|
12 |
+
const response = await fetch(`https://v6.exchangerate-api.com/v6/3ebe2ccf9eeea2aaef280201/latest/${fromCurrency}`);
|
13 |
const data = await response.json();
|
14 |
const rate = data.rates[toCurrency];
|
15 |
const convertedAmount = (amount * rate).toFixed(2);
|
|
|
21 |
convertBtn.style.display = 'none';
|
22 |
} catch (error) {
|
23 |
result.innerText = "Error fetching exchange rates!";
|
24 |
+
console.error('Error fetching exchange rates:', error);
|
25 |
}
|
26 |
}
|
27 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|