.calculator-wrapper {
font-family: sans-serif;
max-width: 600px;
margin: 20px auto;
padding: 20px;
border: 1px solid #ddd;
border-radius: 8px;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
.calculator-form {
margin-bottom: 20px;
}
.calculator-form h2 {
margin-top: 0;
color: #333;
}
.calculator-form p {
color: #555;
line-height: 1.6;
}
.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;
}
button {
background-color: #007bff;
color: white;
padding: 10px 15px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
transition: background-color 0.3s ease;
}
button:hover {
background-color: #0056b3;
}
.calculator-result h3 {
color: #333;
}
.result-value {
font-size: 24px;
font-weight: bold;
color: #28a745;
margin-top: 10px;
}
.explanation {
font-size: 14px;
color: #666;
margin-top: 10px;
line-height: 1.5;
}
function calculateRetentionRate() {
var startCustomers = parseFloat(document.getElementById("customersAtStart").value);
var endCustomers = parseFloat(document.getElementById("customersAtEnd").value);
var newCustomers = parseFloat(document.getElementById("newCustomers").value);
var resultElement = document.getElementById("retentionRateResult");
var explanationElement = document.getElementById("retentionRateExplanation");
if (isNaN(startCustomers) || isNaN(endCustomers) || isNaN(newCustomers) || startCustomers < 0 || endCustomers < 0 || newCustomers < 0) {
resultElement.textContent = "Invalid input. Please enter non-negative numbers.";
explanationElement.textContent = "";
return;
}
if (startCustomers === 0) {
resultElement.textContent = "N/A";
explanationElement.textContent = "Cannot calculate retention rate when there are no customers at the start of the period.";
return;
}
// Formula: Retention Rate = ((Customers at End – New Customers) / Customers at Start) * 100
var retainedCustomers = endCustomers – newCustomers;
var retentionRate = (retainedCustomers / startCustomers) * 100;
if (isNaN(retentionRate)) {
resultElement.textContent = "Error";
explanationElement.textContent = "An error occurred during calculation. Please check your inputs.";
return;
}
resultElement.textContent = retentionRate.toFixed(2) + "%";
var explanation = "The customer retention rate measures the percentage of existing customers who remained with your business over a specific period. ";
explanation += "A higher percentage indicates stronger customer loyalty and satisfaction. ";
explanation += "The formula used is: ((Customers at End – New Customers) / Customers at Start) * 100.";
explanationElement.textContent = explanation;
}