GarGerry commited on
Commit
0645c10
·
verified ·
1 Parent(s): 1b86b44

Update script.js

Browse files
Files changed (1) hide show
  1. script.js +17 -25
script.js CHANGED
@@ -8,48 +8,45 @@ const resultDiv = document.getElementById('result');
8
  // Status konversi otomatis
9
  let autoConvert = false;
10
 
11
- // Tombol Convert ditekan
12
- convertButton.addEventListener('click', () => {
13
- autoConvert = true; // Aktifkan auto-convert
14
- convertCurrency(); // Lakukan konversi
15
- convertButton.style.display = 'none'; // Sembunyikan tombol Convert
16
  });
17
 
18
  // Swap mata uang
19
- swapButton.addEventListener('click', () => {
20
  const temp = fromCurrency.value;
21
  fromCurrency.value = toCurrency.value;
22
  toCurrency.value = temp;
23
 
24
- if (autoConvert) {
25
- convertCurrency(); // Konversi otomatis saat swap
26
- } else {
27
- clearResult(); // Hapus hasil jika auto-convert tidak aktif
28
- }
29
  });
30
 
31
- // Pantau perubahan pada input dan dropdown
32
- [amountInput, fromCurrency, toCurrency].forEach((element) => {
33
- element.addEventListener('input', () => {
34
  if (autoConvert) {
35
- convertCurrency(); // Konversi otomatis saat ada input
36
  }
37
  });
38
 
39
  element.addEventListener('change', () => {
40
- autoConvert = false; // Nonaktifkan auto-convert
41
- convertButton.style.display = 'block'; // Tampilkan tombol Convert
42
- clearResult(); // Hapus hasil sebelumnya
 
43
  });
44
  });
45
 
46
  // Fungsi konversi
47
  async function convertCurrency() {
48
- const amount = parseFloat(amountInput.value);
49
  const from = fromCurrency.value;
50
  const to = toCurrency.value;
51
 
52
- // Validasi input
53
  if (!amount || amount <= 0) {
54
  resultDiv.innerHTML = 'Please enter a valid amount.';
55
  return;
@@ -73,8 +70,3 @@ async function convertCurrency() {
73
  console.error(error);
74
  }
75
  }
76
-
77
- // Fungsi untuk menghapus hasil konversi
78
- function clearResult() {
79
- resultDiv.innerHTML = '';
80
- }
 
8
  // Status konversi otomatis
9
  let autoConvert = false;
10
 
11
+ // Tombol convert pertama kali
12
+ convertButton.addEventListener('click', async () => {
13
+ autoConvert = true; // Aktifkan konversi otomatis
14
+ await convertCurrency();
15
+ convertButton.style.display = 'none'; // Sembunyikan tombol
16
  });
17
 
18
  // Swap mata uang
19
+ swapButton.addEventListener('click', async () => {
20
  const temp = fromCurrency.value;
21
  fromCurrency.value = toCurrency.value;
22
  toCurrency.value = temp;
23
 
24
+ // Langsung konversi ulang jika swap dilakukan
25
+ await convertCurrency();
 
 
 
26
  });
27
 
28
+ // Input perubahan
29
+ [amountInput, fromCurrency, toCurrency].forEach(element => {
30
+ element.addEventListener('input', async () => {
31
  if (autoConvert) {
32
+ await convertCurrency(); // Konversi otomatis
33
  }
34
  });
35
 
36
  element.addEventListener('change', () => {
37
+ // Reset status jika mata uang diubah
38
+ autoConvert = false;
39
+ convertButton.style.display = 'block';
40
+ resultDiv.innerHTML = '';
41
  });
42
  });
43
 
44
  // Fungsi konversi
45
  async function convertCurrency() {
46
+ const amount = amountInput.value;
47
  const from = fromCurrency.value;
48
  const to = toCurrency.value;
49
 
 
50
  if (!amount || amount <= 0) {
51
  resultDiv.innerHTML = 'Please enter a valid amount.';
52
  return;
 
70
  console.error(error);
71
  }
72
  }