function calculateDiscountRate() {
var presentValue = parseFloat(document.getElementById("presentValue").value);
var futureValue = parseFloat(document.getElementById("futureValue").value);
var numberOfPeriods = parseFloat(document.getElementById("numberOfPeriods").value);
var resultDiv = document.getElementById("result");
resultDiv.innerHTML = ""; // Clear previous results
if (isNaN(presentValue) || isNaN(futureValue) || isNaN(numberOfPeriods) ||
presentValue <= 0 || futureValue <= 0 || numberOfPeriods <= 0) {
resultDiv.innerHTML = "Please enter valid positive numbers for all fields.";
return;
}
// The formula for present value is PV = FV / (1 + r)^n
// To find r (the discount rate), we rearrange:
// (1 + r)^n = FV / PV
// 1 + r = (FV / PV)^(1/n)
// r = (FV / PV)^(1/n) – 1
var discountRate = Math.pow((futureValue / presentValue), (1 / numberOfPeriods)) – 1;
if (isNaN(discountRate) || discountRate < 0) {
resultDiv.innerHTML = "Calculation error. Please check your inputs.";
return;
}
var discountRatePercentage = discountRate * 100;
resultDiv.innerHTML = "The required discount rate (r) is:
" + discountRatePercentage.toFixed(2) + "% per period.";
}
.calculator-wrapper {
font-family: 'Arial', sans-serif;
max-width: 600px;
margin: 20px auto;
padding: 20px;
border: 1px solid #ddd;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
background-color: #fff;
}
.calculator-form {
margin-bottom: 20px;
padding-bottom: 20px;
border-bottom: 1px solid #eee;
}
.calculator-form h2 {
text-align: center;
color: #333;
margin-bottom: 15px;
}
.calculator-form p {
font-size: 0.95em;
color: #555;
line-height: 1.5;
margin-bottom: 20px;
}
.form-group {
margin-bottom: 15px;
}
.form-group label {
display: block;
margin-bottom: 5px;
font-weight: bold;
color: #444;
}
.form-group input[type="number"] {
width: 100%;
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box; /* Include padding and border in the element's total width and height */
font-size: 1em;
}
.form-group input[type="number"]:focus {
border-color: #007bff;
outline: none;
box-shadow: 0 0 0 2px rgba(0, 123, 255, 0.25);
}
button {
display: block;
width: 100%;
padding: 12px 20px;
background-color: #007bff;
color: white;
border: none;
border-radius: 5px;
font-size: 1.1em;
cursor: pointer;
transition: background-color 0.3s ease;
margin-top: 10px;
}
button:hover {
background-color: #0056b3;
}
.calculator-result {
text-align: center;
}
.calculator-result h3 {
color: #333;
margin-bottom: 10px;
}
#result p {
font-size: 1.1em;
color: #333;
}
#result strong {
font-size: 1.2em;
}