Spaces:
Running
Running
Update script.js
Browse files
script.js
CHANGED
@@ -1,64 +1,51 @@
|
|
1 |
-
//
|
2 |
async function convertCurrency() {
|
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 |
function swapCurrencies() {
|
30 |
-
|
31 |
-
|
32 |
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
|
38 |
-
|
39 |
-
|
40 |
}
|
41 |
|
42 |
-
// Event
|
43 |
-
document.getElementById('amount').addEventListener('input',
|
44 |
-
// Menampilkan tombol Convert hanya jika pertama kali konversi
|
45 |
-
const convertBtn = document.getElementById('convert-btn');
|
46 |
-
convertBtn.style.display = 'block';
|
47 |
-
document.getElementById('result').innerHTML = ''; // Menghapus hasil konversi sebelumnya
|
48 |
-
});
|
49 |
-
|
50 |
-
// Event listener untuk perubahan mata uang (From dan To) agar tombol Convert muncul kembali
|
51 |
document.getElementById('from-currency').addEventListener('change', () => {
|
52 |
-
|
53 |
-
|
54 |
});
|
55 |
|
56 |
document.getElementById('to-currency').addEventListener('change', () => {
|
57 |
-
|
58 |
-
|
59 |
-
});
|
60 |
-
|
61 |
-
// Event listener untuk tombol Convert, agar hanya konversi sekali
|
62 |
-
document.getElementById('convert-btn').addEventListener('click', () => {
|
63 |
-
convertCurrency();
|
64 |
});
|
|
|
1 |
+
// Function to perform currency conversion
|
2 |
async function convertCurrency() {
|
3 |
+
const amount = document.getElementById('amount').value;
|
4 |
+
const fromCurrency = document.getElementById('from-currency').value;
|
5 |
+
const toCurrency = document.getElementById('to-currency').value;
|
6 |
+
const result = document.getElementById('result');
|
7 |
+
const convertBtn = document.getElementById('convert-btn');
|
8 |
+
|
9 |
+
if (amount && fromCurrency && toCurrency) {
|
10 |
+
try {
|
11 |
+
const response = await fetch(`https://api.exchangerate-api.com/v4/latest/${fromCurrency}`);
|
12 |
+
const data = await response.json();
|
13 |
+
const rate = data.rates[toCurrency];
|
14 |
+
const convertedAmount = (amount * rate).toFixed(2);
|
15 |
+
|
16 |
+
// Display conversion result
|
17 |
+
result.innerHTML = `${amount} ${fromCurrency} = ${convertedAmount} ${toCurrency}`;
|
18 |
+
|
19 |
+
// Hide Convert button after first use
|
20 |
+
convertBtn.style.display = 'none';
|
21 |
+
} catch (error) {
|
22 |
+
result.innerText = "Error fetching exchange rates!";
|
23 |
+
}
|
24 |
}
|
|
|
25 |
}
|
26 |
|
27 |
+
// Function to swap currencies
|
28 |
function swapCurrencies() {
|
29 |
+
const fromCurrency = document.getElementById('from-currency');
|
30 |
+
const toCurrency = document.getElementById('to-currency');
|
31 |
|
32 |
+
// Swap currency values
|
33 |
+
const temp = fromCurrency.value;
|
34 |
+
fromCurrency.value = toCurrency.value;
|
35 |
+
toCurrency.value = temp;
|
36 |
|
37 |
+
// Perform automatic conversion after swapping
|
38 |
+
convertCurrency();
|
39 |
}
|
40 |
|
41 |
+
// Event listeners for input amount and currency selection
|
42 |
+
document.getElementById('amount').addEventListener('input', convertCurrency);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
document.getElementById('from-currency').addEventListener('change', () => {
|
44 |
+
document.getElementById('convert-btn').style.display = 'block';
|
45 |
+
document.getElementById('result').innerHTML = '';
|
46 |
});
|
47 |
|
48 |
document.getElementById('to-currency').addEventListener('change', () => {
|
49 |
+
document.getElementById('convert-btn').style.display = 'block';
|
50 |
+
document.getElementById('result').innerHTML = '';
|
|
|
|
|
|
|
|
|
|
|
51 |
});
|