0% found this document useful (0 votes)
4 views

script.js

The document contains a JavaScript code for a currency converter that fetches exchange rates from an API. It initializes currency options for selection and provides functionality to convert an amount from one currency to another based on user input. The code also handles the display of the conversion result on the webpage.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

script.js

The document contains a JavaScript code for a currency converter that fetches exchange rates from an API. It initializes currency options for selection and provides functionality to convert an amount from one currency to another based on user input. The code also handles the display of the conversion result on the webpage.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

3/22/25, 2:00 PM script.

js

script.js

1 const apiURL = 'https://api.exchangerate-api.com/v4/latest/';


2
3 async function loadCurrencies() {
4 const res = await fetch(apiURL + 'USD');
5 const data = await res.json();
6 const currencies = Object.keys(data.rates);
7 const fromSelect = document.getElementById('from-currency');
8 const toSelect = document.getElementById('to-currency');
9
10 currencies.forEach(currency => {
11 const option1 = document.createElement('option');
12 option1.value = currency;
13 option1.textContent = currency;
14 fromSelect.appendChild(option1);
15
16 const option2 = option1.cloneNode(true);
17 toSelect.appendChild(option2);
18 });
19
20 fromSelect.value = 'USD';
21 toSelect.value = 'INR';
22 }
23
24 async function convertCurrency() {
25 const amount = document.getElementById('amount').value;
26 const fromCurrency = document.getElementById('from-currency').value;
27 const toCurrency = document.getElementById('to-currency').value;
28
29 if (!amount) return alert('Please enter an amount');
30
31 const res = await fetch(apiURL + fromCurrency);
32 const data = await res.json();
33 const rate = data.rates[toCurrency];
34 const result = (amount * rate).toFixed(2);
35
36 document.getElementById('result').textContent = `${amount} ${fromCurrency} = ${result}
${toCurrency}`;
37 }
38
39 loadCurrencies();
40

localhost:3134/710bd581-9852-4c4f-94fd-da439a19bacb/ 1/1

You might also like