File size: 2,880 Bytes
6f61b53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
document.getElementById('currencyForm').addEventListener('submit', async function (event) {
  event.preventDefault();

  // Ambil nilai input dari user
  const amount = document.getElementById('amount').value;
  const fromCurrency = document.getElementById('fromCurrency').value;
  const toCurrency = document.getElementById('toCurrency').value;
  const resultDiv = document.getElementById('result');

  // Validasi jumlah yang dimasukkan
  if (!amount || amount <= 0) {
    resultDiv.innerHTML = "Please enter a valid amount!";
    return;
  }

  try {
    const apiKey = '3ebe2ccf9eeea2aaef280201'; // API Key
    const url = `https://v6.exchangerate-api.com/v6/${apiKey}/latest/${fromCurrency}`;

    // Mengambil data dari API
    const response = await fetch(url);
    const data = await response.json();

    // Menangani respon API yang error
    if (data.result === 'error') {
      resultDiv.innerHTML = `Error: ${data['error-type']}`;
    } else {
      // Menyusun nilai konversi dan menampilkannya
      const rate = data.conversion_rates[toCurrency];
      const convertedAmount = (amount * rate).toFixed(2);
      resultDiv.innerHTML = `${amount} ${fromCurrency} = ${convertedAmount} ${toCurrency}`;
    }
  } catch (error) {
    resultDiv.innerHTML = "Error fetching conversion rate.";
    console.error("Error:", error); // Menampilkan error di konsol
  }
});

// Logika untuk tombol Swap
document.getElementById('swapButton').addEventListener('click', async function () {
  const fromCurrency = document.getElementById('fromCurrency');
  const toCurrency = document.getElementById('toCurrency');
  const resultDiv = document.getElementById('result');

  // Tukar nilai antara "From" dan "To"
  const tempCurrency = fromCurrency.value;
  fromCurrency.value = toCurrency.value;
  toCurrency.value = tempCurrency;

  // Ambil nilai input amount
  const amount = document.getElementById('amount').value;

  // Jika ada jumlah yang dimasukkan, langsung lakukan konversi ulang
  if (amount && amount > 0) {
    try {
      const apiKey = '3ebe2ccf9eeea2aaef280201'; // API Key
      const url = `https://v6.exchangerate-api.com/v6/${apiKey}/latest/${fromCurrency.value}`;

      // Mengambil data dari API
      const response = await fetch(url);
      const data = await response.json();

      if (data.result === 'error') {
        resultDiv.innerHTML = `Error: ${data['error-type']}`;
      } else {
        const rate = data.conversion_rates[toCurrency.value];
        const convertedAmount = (amount * rate).toFixed(2);
        resultDiv.innerHTML = `${amount} ${fromCurrency.value} = ${convertedAmount} ${toCurrency.value}`;
      }
    } catch (error) {
      resultDiv.innerHTML = "Error fetching conversion rate.";
      console.error("Error:", error);
    }
  } else {
    // Jika tidak ada amount, hapus hasil konversi
    resultDiv.innerHTML = "";
  }
});