Update script.js
Browse files
script.js
CHANGED
@@ -1,50 +1,55 @@
|
|
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 |
function swapCurrencies() {
|
27 |
-
const fromCurrency = document.getElementById('from-currency');
|
28 |
-
const toCurrency = document.getElementById('to-currency');
|
29 |
-
|
30 |
-
// Swap currency values
|
31 |
const temp = fromCurrency.value;
|
32 |
fromCurrency.value = toCurrency.value;
|
33 |
toCurrency.value = temp;
|
34 |
-
|
35 |
-
// Automatically convert after swap
|
36 |
-
convertCurrency();
|
37 |
}
|
38 |
|
39 |
-
// Event
|
40 |
-
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
document.getElementById('result').innerHTML = '';
|
45 |
-
});
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
1 |
+
// Mengambil elemen-elemen dari DOM
|
2 |
+
const amountInput = document.getElementById("amount");
|
3 |
+
const fromCurrency = document.getElementById("from");
|
4 |
+
const toCurrency = document.getElementById("to");
|
5 |
+
const convertButton = document.getElementById("convert-btn");
|
6 |
+
const swapButton = document.getElementById("swap-btn");
|
7 |
+
const result = document.getElementById("result");
|
8 |
+
|
9 |
+
// Data kurs mata uang (contoh)
|
10 |
+
const exchangeRates = {
|
11 |
+
"USD": { "EUR": 0.85, "IDR": 14200, "GBP": 0.75, "MYR": 4.20 },
|
12 |
+
"EUR": { "USD": 1.18, "IDR": 16750, "GBP": 0.88, "MYR": 4.94 },
|
13 |
+
"IDR": { "USD": 0.000070, "EUR": 0.000060, "GBP": 0.000053, "MYR": 0.00025 },
|
14 |
+
"GBP": { "USD": 1.33, "EUR": 1.14, "IDR": 18850, "MYR": 5.60 },
|
15 |
+
"MYR": { "USD": 0.24, "EUR": 0.20, "IDR": 3700, "GBP": 0.18 }
|
16 |
+
};
|
17 |
+
|
18 |
+
// Fungsi untuk menghitung konversi mata uang
|
19 |
+
function convertCurrency() {
|
20 |
+
const amount = parseFloat(amountInput.value);
|
21 |
+
const from = fromCurrency.value;
|
22 |
+
const to = toCurrency.value;
|
23 |
+
|
24 |
+
if (isNaN(amount) || amount <= 0) {
|
25 |
+
result.textContent = "Please enter a valid amount.";
|
26 |
+
return;
|
27 |
+
}
|
28 |
+
|
29 |
+
const rate = exchangeRates[from][to];
|
30 |
+
if (rate) {
|
31 |
+
const convertedAmount = (amount * rate).toFixed(2);
|
32 |
+
result.textContent = `${amount} ${from} = ${convertedAmount} ${to}`;
|
33 |
+
} else {
|
34 |
+
result.textContent = "Conversion rate not available.";
|
35 |
}
|
36 |
}
|
37 |
|
38 |
+
// Fungsi untuk menukar mata uang
|
39 |
function swapCurrencies() {
|
|
|
|
|
|
|
|
|
40 |
const temp = fromCurrency.value;
|
41 |
fromCurrency.value = toCurrency.value;
|
42 |
toCurrency.value = temp;
|
43 |
+
convertCurrency(); // Setelah swap, langsung hitung konversinya
|
|
|
|
|
44 |
}
|
45 |
|
46 |
+
// Event listener untuk tombol Convert
|
47 |
+
convertButton.addEventListener("click", convertCurrency);
|
48 |
|
49 |
+
// Event listener untuk tombol Swap
|
50 |
+
swapButton.addEventListener("click", swapCurrencies);
|
|
|
|
|
51 |
|
52 |
+
// Optionally, bisa tambahkan fitur untuk menghitung otomatis ketika ada perubahan input
|
53 |
+
amountInput.addEventListener("input", convertCurrency);
|
54 |
+
fromCurrency.addEventListener("change", convertCurrency);
|
55 |
+
toCurrency.addEventListener("change", convertCurrency);
|