function calculateCompoundInterest() {
var principal = parseFloat(document.getElementById("principal").value);
var annualInterestRate = parseFloat(document.getElementById("annualInterestRate").value);
var compoundingFrequency = parseInt(document.getElementById("compoundingFrequency").value);
var timeInYears = parseFloat(document.getElementById("timeInYears").value);
var resultDiv = document.getElementById("result");
resultDiv.innerHTML = "; // Clear previous results
if (isNaN(principal) || isNaN(annualInterestRate) || isNaN(compoundingFrequency) || isNaN(timeInYears) ||
principal < 0 || annualInterestRate < 0 || compoundingFrequency <= 0 || timeInYears < 0) {
resultDiv.innerHTML = "Please enter valid positive numbers for all fields.";
return;
}
var ratePerPeriod = annualInterestRate / 100 / compoundingFrequency;
var numberOfPeriods = compoundingFrequency * timeInYears;
var futureValue = principal * Math.pow((1 + ratePerPeriod), numberOfPeriods);
var totalInterestEarned = futureValue – principal;
resultDiv.innerHTML = "
Calculation Results
Initial Investment: $" + principal.toFixed(2) + "
Annual Interest Rate: " + annualInterestRate.toFixed(2) + "%
Compounding Frequency: " + getFrequencyDescription(compoundingFrequency) + "
Time Period: " + timeInYears + " years
Future Value: $" + futureValue.toFixed(2) + "
Total Interest Earned: $" + totalInterestEarned.toFixed(2) + "
";
}
function getFrequencyDescription(frequency) {
switch(frequency) {
case 1: return "Annually";
case 2: return "Semi-annually";
case 4: return "Quarterly";
case 12: return "Monthly";
case 365: return "Daily";
default: return "Custom";
}
}
.calculator-container {
font-family: Arial, sans-serif;
max-width: 600px;
margin: 20px auto;
padding: 20px;
border: 1px solid #ccc;
border-radius: 8px;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
background-color: #f9f9f9;
}
.calculator-container h2 {
text-align: center;
margin-bottom: 15px;
color: #333;
}
.calculator-container p {
font-size: 0.95em;
line-height: 1.5;
color: #555;
text-align: justify;
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"],
.form-group select {
width: calc(100% – 12px);
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 select {
height: 38px; /* Align height with input fields */
}
button {
display: block;
width: 100%;
padding: 10px 15px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 1.1em;
margin-top: 20px;
transition: background-color 0.3s ease;
}
button:hover {
background-color: #45a049;
}
.calculator-result {
margin-top: 30px;
padding: 20px;
border: 1px solid #eee;
border-radius: 4px;
background-color: #fff;
}
.calculator-result h3 {
margin-top: 0;
color: #333;
border-bottom: 1px solid #eee;
padding-bottom: 10px;
margin-bottom: 15px;
}
.calculator-result p {
margin-bottom: 10px;
color: #555;
font-size: 1em;
text-align: left;
}
.calculator-result p strong {
color: #222;
}