Exchange rates are the value of one nation's currency for the purpose of trade of another. They are determined by supply and demand in the foreign exchange market. Factors influencing these rates include interest rates, inflation, political stability, economic performance, and speculation.
function calculateExchangeRate() {
var baseAmount = parseFloat(document.getElementById("baseCurrencyAmount").value);
var baseCurrency = document.getElementById("baseCurrencyName").value.toUpperCase();
var targetCurrency = document.getElementById("targetCurrencyName").value.toUpperCase();
var resultDiv = document.getElementById("result");
resultDiv.innerHTML = ""; // Clear previous results
if (isNaN(baseAmount) || baseAmount response.json())
// .then(data => {
// if (data.rates && data.rates[targetCurrency]) {
// var rate = data.rates[targetCurrency];
// var convertedAmount = baseAmount * rate;
// resultDiv.innerHTML =
// baseAmount + " " + baseCurrency + " is equal to " +
// convertedAmount.toFixed(2) + " " + targetCurrency +
// "Exchange Rate: 1 " + baseCurrency + " = " + rate.toFixed(4) + " " + targetCurrency;
// } else {
// resultDiv.innerHTML = "Could not fetch exchange rate for " + baseCurrency + "/" + targetCurrency + ". Please check currency codes.";
// }
// })
// .catch(error => {
// resultDiv.innerHTML = "Error fetching exchange rate: " + error;
// });
// — End Placeholder —
// — Simplified Calculation for Demonstration —
// This section simulates a calculation. In a real app, this would be replaced
// by an API call to get the actual exchange rate.
var simulatedRates = {
"USD": {"EUR": 0.92, "JPY": 150.00, "GBP": 0.79},
"EUR": {"USD": 1.08, "JPY": 163.00, "GBP": 0.86},
"JPY": {"USD": 0.0067, "EUR": 0.0061, "GBP": 0.0053},
"GBP": {"USD": 1.27, "EUR": 1.16, "JPY": 189.00}
};
var rate = 0;
if (simulatedRates[baseCurrency] && simulatedRates[baseCurrency][targetCurrency]) {
rate = simulatedRates[baseCurrency][targetCurrency];
var convertedAmount = baseAmount * rate;
resultDiv.innerHTML =
baseAmount + " " + baseCurrency + " is equal to " +
convertedAmount.toFixed(2) + " " + targetCurrency +
"Exchange Rate: 1 " + baseCurrency + " = " + rate.toFixed(4) + " " + targetCurrency;
} else if (baseCurrency === targetCurrency) {
rate = 1;
var convertedAmount = baseAmount * rate;
resultDiv.innerHTML =
baseAmount + " " + baseCurrency + " is equal to " +
convertedAmount.toFixed(2) + " " + targetCurrency +
"Exchange Rate: 1 " + baseCurrency + " = " + rate.toFixed(4) + " " + targetCurrency;
}
else {
resultDiv.innerHTML = "Simulated exchange rate not available for " + baseCurrency + "/" + targetCurrency + ". Please use valid currency codes (USD, EUR, JPY, GBP) for this demo.";
}
// — End Simplified Calculation —
}
#exchange-rate-calculator {
font-family: sans-serif;
border: 1px solid #ccc;
padding: 20px;
border-radius: 8px;
max-width: 500px;
margin: 20px auto;
background-color: #f9f9f9;
}
.input-group {
margin-bottom: 15px;
}
.input-group label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}
.input-group input[type="number"],
.input-group input[type="text"] {
width: calc(100% – 12px);
padding: 8px;
border: 1px solid #ccc;
border-radius: 4px;
}
button {
background-color: #4CAF50;
color: white;
padding: 10px 15px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
}
button:hover {
background-color: #45a049;
}
#result {
margin-top: 20px;
padding: 15px;
background-color: #e0ffe0;
border: 1px solid #d0d0d0;
border-radius: 4px;
font-size: 1.1em;
text-align: center;
}