-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
1 lines (1 loc) · 1.41 KB
/
Copy pathscript.js
File metadata and controls
1 lines (1 loc) · 1.41 KB
1
let api=`https://v6.exchangerate-api.com/v6/${apiKey}/latest/USD`;const fromDropDown=document.getElementById("from-currency-select");const toDropDown=document.getElementById("to-currency-select");currencies.forEach((currency)=>{const option=document.createElement("option"); option.value=currency; option.text=currency; fromDropDown.add(option);});currencies.forEach((currency)=>{const option=document.createElement("option"); option.value=currency; option.text=currency; toDropDown.add(option);});fromDropDown.value="USD";toDropDown.value="EGP";let convertCurrency=()=>{const amount=document.querySelector("#amount").value; const fromCurrency=fromDropDown.value; const toCurrency=toDropDown.value; if (amount.length !=0){fetch(api) .then((resp)=> resp.json()) .then((data)=>{let fromExchangeRate=data.conversion_rates[fromCurrency]; let toExchangeRate=data.conversion_rates[toCurrency]; const convertedAmount=(amount / fromExchangeRate) * toExchangeRate; result.innerHTML=`${amount}${fromCurrency}=${convertedAmount.toFixed( 2 )}${toCurrency}`;});}else{alert("Please fill in the amount");}};document .querySelector("#convert-button") .addEventListener("click", convertCurrency);window.addEventListener("load", convertCurrency);const exchangeIcon=document.getElementById('exchange-icon');exchangeIcon.addEventListener('click', ()=>{const tempCurrency=fromDropDown.value; fromDropDown.value=toDropDown.value; toDropDown.value=tempCurrency;});