GarGerry commited on
Commit
641c42f
·
verified ·
1 Parent(s): 5485a06

Update script.js

Browse files
Files changed (1) hide show
  1. script.js +23 -10
script.js CHANGED
@@ -8,11 +8,11 @@ const resultDiv = document.getElementById('result');
8
  // Status konversi otomatis
9
  let autoConvert = false;
10
 
11
- // Tombol convert pertama kali
12
  convertButton.addEventListener('click', () => {
13
- autoConvert = true; // Aktifkan konversi otomatis
14
  convertCurrency(); // Lakukan konversi pertama kali
15
- convertButton.style.display = 'none'; // Sembunyikan tombol
16
  });
17
 
18
  // Swap mata uang
@@ -25,8 +25,7 @@ swapButton.addEventListener('click', async () => {
25
  if (autoConvert) {
26
  convertCurrency();
27
  } else {
28
- // Jika belum autoConvert, kosongkan hasil
29
- resultDiv.innerHTML = '';
30
  }
31
  });
32
 
@@ -34,15 +33,14 @@ swapButton.addEventListener('click', async () => {
34
  [amountInput, fromCurrency, toCurrency].forEach((element) => {
35
  element.addEventListener('input', () => {
36
  if (autoConvert) {
37
- convertCurrency(); // Konversi otomatis saat autoConvert aktif
38
  }
39
  });
40
 
41
  element.addEventListener('change', () => {
42
- // Tampilkan tombol Convert jika ada perubahan manual
43
- autoConvert = false;
44
- convertButton.style.display = 'block';
45
- resultDiv.innerHTML = '';
46
  });
47
  });
48
 
@@ -76,3 +74,18 @@ async function convertCurrency() {
76
  console.error(error);
77
  }
78
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  // Status konversi otomatis
9
  let autoConvert = false;
10
 
11
+ // Tombol Convert pertama kali ditekan
12
  convertButton.addEventListener('click', () => {
13
+ autoConvert = true; // Aktifkan auto-convert
14
  convertCurrency(); // Lakukan konversi pertama kali
15
+ hideConvertButton(); // Sembunyikan tombol Convert
16
  });
17
 
18
  // Swap mata uang
 
25
  if (autoConvert) {
26
  convertCurrency();
27
  } else {
28
+ clearResult(); // Hapus hasil jika autoConvert tidak aktif
 
29
  }
30
  });
31
 
 
33
  [amountInput, fromCurrency, toCurrency].forEach((element) => {
34
  element.addEventListener('input', () => {
35
  if (autoConvert) {
36
+ convertCurrency(); // Konversi otomatis
37
  }
38
  });
39
 
40
  element.addEventListener('change', () => {
41
+ autoConvert = false; // Nonaktifkan auto-convert
42
+ showConvertButton(); // Tampilkan tombol Convert kembali
43
+ clearResult(); // Hapus hasil sebelumnya
 
44
  });
45
  });
46
 
 
74
  console.error(error);
75
  }
76
  }
77
+
78
+ // Fungsi untuk menyembunyikan tombol Convert
79
+ function hideConvertButton() {
80
+ convertButton.style.display = 'none';
81
+ }
82
+
83
+ // Fungsi untuk menampilkan tombol Convert
84
+ function showConvertButton() {
85
+ convertButton.style.display = 'block';
86
+ }
87
+
88
+ // Fungsi untuk menghapus hasil konversi
89
+ function clearResult() {
90
+ resultDiv.innerHTML = '';
91
+ }