GarGerry commited on
Commit
5c4f4ba
·
verified ·
1 Parent(s): e1a6a68

Update script.js

Browse files
Files changed (1) hide show
  1. script.js +17 -7
script.js CHANGED
@@ -1,3 +1,4 @@
 
1
  async function convertCurrency() {
2
  const amount = document.getElementById('amount').value;
3
  const fromCurrency = document.getElementById('from-currency').value;
@@ -7,38 +8,47 @@ async function convertCurrency() {
7
 
8
  if (amount && fromCurrency && toCurrency) {
9
  try {
10
- const response = await fetch(`https://api.exchangerate-api.com/v4/latest/${fromCurrency}`);
 
11
  const data = await response.json();
 
 
 
 
 
12
  const rate = data.rates[toCurrency];
13
  const convertedAmount = (amount * rate).toFixed(2);
14
 
15
- // Display conversion result
16
  result.innerHTML = `${amount} ${fromCurrency} = ${convertedAmount} ${toCurrency}`;
17
 
18
- // Hide convert button after conversion
19
  convertBtn.style.display = 'none';
20
  } catch (error) {
21
  result.innerText = "Error fetching exchange rates!";
 
22
  }
23
  }
24
  }
25
 
 
26
  function swapCurrencies() {
27
  const fromCurrency = document.getElementById('from-currency');
28
  const toCurrency = document.getElementById('to-currency');
29
 
30
- // Swap currency values
31
  const temp = fromCurrency.value;
32
  fromCurrency.value = toCurrency.value;
33
  toCurrency.value = temp;
34
 
35
- // Automatically convert after swap
36
  convertCurrency();
37
  }
38
 
39
- // Event listeners for input and currency change
40
  document.getElementById('amount').addEventListener('input', convertCurrency);
41
 
 
42
  document.getElementById('from-currency').addEventListener('change', () => {
43
  document.getElementById('convert-btn').style.display = 'block';
44
  document.getElementById('result').innerHTML = '';
@@ -47,4 +57,4 @@ document.getElementById('from-currency').addEventListener('change', () => {
47
  document.getElementById('to-currency').addEventListener('change', () => {
48
  document.getElementById('convert-btn').style.display = 'block';
49
  document.getElementById('result').innerHTML = '';
50
- });
 
1
+ // Fungsi untuk melakukan konversi mata uang
2
  async function convertCurrency() {
3
  const amount = document.getElementById('amount').value;
4
  const fromCurrency = document.getElementById('from-currency').value;
 
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 = '';
 
57
  document.getElementById('to-currency').addEventListener('change', () => {
58
  document.getElementById('convert-btn').style.display = 'block';
59
  document.getElementById('result').innerHTML = '';
60
+ });