GarGerry commited on
Commit
44ac58c
·
verified ·
1 Parent(s): 13daff1

Update script.js

Browse files
Files changed (1) hide show
  1. script.js +36 -3
script.js CHANGED
@@ -8,20 +8,53 @@ async function convertCurrency() {
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);
16
 
17
  // Menampilkan hasil konversi
18
  result.innerHTML = `${amount} ${fromCurrency} = ${convertedAmount} ${toCurrency}`;
19
 
20
- // Sembunyikan tombol Convert setelah konversi otomatis
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
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
  if (amount && fromCurrency && toCurrency) {
10
  try {
11
+ // Menggunakan API dengan API key yang benar
12
  const response = await fetch(`https://v6.exchangerate-api.com/v6/3ebe2ccf9eeea2aaef280201/latest/${fromCurrency}`);
13
  const data = await response.json();
14
+
15
+ if (data.result === 'error') {
16
+ throw new Error('Error fetching exchange rates!');
17
+ }
18
+
19
  const rate = data.rates[toCurrency];
20
  const convertedAmount = (amount * rate).toFixed(2);
21
 
22
  // Menampilkan hasil konversi
23
  result.innerHTML = `${amount} ${fromCurrency} = ${convertedAmount} ${toCurrency}`;
24
 
25
+ // Menyembunyikan tombol Convert setelah konversi otomatis
26
  convertBtn.style.display = 'none';
27
  } catch (error) {
28
  result.innerText = "Error fetching exchange rates!";
29
  console.error('Error fetching exchange rates:', error);
30
  }
31
  }
32
+ }
33
+
34
+ // Fungsi untuk menukar mata uang From dan To
35
+ function swapCurrencies() {
36
+ const fromCurrency = document.getElementById('from-currency');
37
+ const toCurrency = document.getElementById('to-currency');
38
+
39
+ // Tukar nilai mata uang
40
+ const temp = fromCurrency.value;
41
+ fromCurrency.value = toCurrency.value;
42
+ toCurrency.value = temp;
43
+
44
+ // Lakukan konversi otomatis setelah swap
45
+ convertCurrency();
46
+ }
47
+
48
+ // Event listener untuk input amount agar otomatis konversi
49
+ document.getElementById('amount').addEventListener('input', convertCurrency);
50
+
51
+ // Event listener untuk perubahan mata uang (From dan To) agar tombol Convert muncul kembali
52
+ document.getElementById('from-currency').addEventListener('change', () => {
53
+ document.getElementById('convert-btn').style.display = 'block';
54
+ document.getElementById('result').innerHTML = '';
55
+ });
56
+
57
+ document.getElementById('to-currency').addEventListener('change', () => {
58
+ document.getElementById('convert-btn').style.display = 'block';
59
+ document.getElementById('result').innerHTML = '';
60
+ });