GarGerry commited on
Commit
d58e416
·
verified ·
1 Parent(s): d56f01c

Update script.js

Browse files
Files changed (1) hide show
  1. script.js +8 -5
script.js CHANGED
@@ -1,34 +1,37 @@
1
  document.getElementById('currencyForm').addEventListener('submit', async function(event) {
2
  event.preventDefault();
3
 
 
4
  const amount = document.getElementById('amount').value;
5
  const fromCurrency = document.getElementById('fromCurrency').value;
6
  const toCurrency = document.getElementById('toCurrency').value;
7
  const resultDiv = document.getElementById('result');
8
 
 
9
  if (!amount || amount <= 0) {
10
  resultDiv.innerHTML = "Please enter a valid amount!";
11
  return;
12
  }
13
 
14
  try {
15
- const apiKey = '3ebe2ccf9eeea2aaef280201'; // API Key yang kamu berikan
16
  const url = `https://v6.exchangerate-api.com/v6/${apiKey}/latest/${fromCurrency}`;
 
 
17
  const response = await fetch(url);
18
  const data = await response.json();
19
 
20
- // Log respons API untuk debugging
21
- console.log(data);
22
-
23
  if (data.result === 'error') {
24
  resultDiv.innerHTML = `Error: ${data['error-type']}`;
25
  } else {
 
26
  const rate = data.conversion_rates[toCurrency];
27
  const convertedAmount = (amount * rate).toFixed(2);
28
  resultDiv.innerHTML = `${amount} ${fromCurrency} = ${convertedAmount} ${toCurrency}`;
29
  }
30
  } catch (error) {
31
  resultDiv.innerHTML = "Error fetching conversion rate.";
32
- console.error(error); // Log error jika ada
33
  }
34
  });
 
1
  document.getElementById('currencyForm').addEventListener('submit', async function(event) {
2
  event.preventDefault();
3
 
4
+ // Ambil nilai input dari user
5
  const amount = document.getElementById('amount').value;
6
  const fromCurrency = document.getElementById('fromCurrency').value;
7
  const toCurrency = document.getElementById('toCurrency').value;
8
  const resultDiv = document.getElementById('result');
9
 
10
+ // Validasi jumlah yang dimasukkan
11
  if (!amount || amount <= 0) {
12
  resultDiv.innerHTML = "Please enter a valid amount!";
13
  return;
14
  }
15
 
16
  try {
17
+ const apiKey = '3ebe2ccf9eeea2aaef280201'; // API Key
18
  const url = `https://v6.exchangerate-api.com/v6/${apiKey}/latest/${fromCurrency}`;
19
+
20
+ // Mengambil data dari API
21
  const response = await fetch(url);
22
  const data = await response.json();
23
 
24
+ // Menangani respon API yang error
 
 
25
  if (data.result === 'error') {
26
  resultDiv.innerHTML = `Error: ${data['error-type']}`;
27
  } else {
28
+ // Menyusun nilai konversi dan menampilkannya
29
  const rate = data.conversion_rates[toCurrency];
30
  const convertedAmount = (amount * rate).toFixed(2);
31
  resultDiv.innerHTML = `${amount} ${fromCurrency} = ${convertedAmount} ${toCurrency}`;
32
  }
33
  } catch (error) {
34
  resultDiv.innerHTML = "Error fetching conversion rate.";
35
+ console.error("Error:", error); // Menampilkan error di konsol
36
  }
37
  });