-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
25 lines (20 loc) · 1019 Bytes
/
Copy pathscript.js
File metadata and controls
25 lines (20 loc) · 1019 Bytes
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
const apiKey = '9b6ae85e0ff03048dac94242'; // Replace with your API key from ExchangeRate-API
const apiUrl = `https://v6.exchangerate-api.com/v6/${apiKey}/latest/`;
document.getElementById('convert-btn').addEventListener('click', async () => {
const fromCurrency = document.getElementById('from-currency').value;
const toCurrency = document.getElementById('to-currency').value;
const amount = document.getElementById('amount').value;
if (!amount || isNaN(amount)) {
alert('Please enter a valid amount!');
return;
}
const response = await fetch(`${apiUrl}${fromCurrency}`);
const data = await response.json();
if (data.result === 'success') {
const conversionRate = data.conversion_rates[toCurrency];
const convertedAmount = (amount * conversionRate).toFixed(2);
document.getElementById('converted-amount').textContent = `${convertedAmount} ${toCurrency}`;
} else {
alert('Error fetching data. Please try again later.');
}
});