GarGerry commited on
Commit
52bf539
·
verified ·
1 Parent(s): 5c3a9b3

Create script.js

Browse files
Files changed (1) hide show
  1. script.js +28 -0
script.js ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ async function convertCurrency() {
2
+ const amount = document.getElementById('amount').value;
3
+ const fromCurrency = document.getElementById('fromCurrency').value;
4
+ const toCurrency = document.getElementById('toCurrency').value;
5
+ const resultDiv = document.getElementById('result');
6
+
7
+ if (!amount || amount <= 0) {
8
+ resultDiv.innerHTML = "Please enter a valid amount!";
9
+ return;
10
+ }
11
+
12
+ try {
13
+ const apiKey = 'YOUR_API_KEY'; // Replace with your API key
14
+ const url = `https://v6.exchangeratesapi.io/latest?base=${fromCurrency}&symbols=${toCurrency}&access_key=${apiKey}`;
15
+ const response = await fetch(url);
16
+ const data = await response.json();
17
+
18
+ if (data.error) {
19
+ resultDiv.innerHTML = "Error fetching data.";
20
+ } else {
21
+ const rate = data.rates[toCurrency];
22
+ const convertedAmount = (amount * rate).toFixed(2);
23
+ resultDiv.innerHTML = `${amount} ${fromCurrency} = ${convertedAmount} ${toCurrency}`;
24
+ }
25
+ } catch (error) {
26
+ resultDiv.innerHTML = "Error fetching conversion rate.";
27
+ }
28
+ }