File size: 1,049 Bytes
52bf539
 
 
 
 
 
 
 
 
 
 
 
f0e381a
52bf539
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
async function convertCurrency() {
  const amount = document.getElementById('amount').value;
  const fromCurrency = document.getElementById('fromCurrency').value;
  const toCurrency = document.getElementById('toCurrency').value;
  const resultDiv = document.getElementById('result');

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

  try {
    const apiKey = '3ebe2ccf9eeea2aaef280201'; // API Key yang kamu berikan
    const url = `https://v6.exchangeratesapi.io/latest?base=${fromCurrency}&symbols=${toCurrency}&access_key=${apiKey}`;
    const response = await fetch(url);
    const data = await response.json();

    if (data.error) {
      resultDiv.innerHTML = "Error fetching data.";
    } else {
      const rate = data.rates[toCurrency];
      const convertedAmount = (amount * rate).toFixed(2);
      resultDiv.innerHTML = `${amount} ${fromCurrency} = ${convertedAmount} ${toCurrency}`;
    }
  } catch (error) {
    resultDiv.innerHTML = "Error fetching conversion rate.";
  }
}