function calculateCompoundInterest() {
var principal = parseFloat(document.getElementById("principal").value);
var annualRate = parseFloat(document.getElementById("annualRate").value);
var years = parseFloat(document.getElementById("years").value);
var compoundingFrequency = parseInt(document.getElementById("compoundingFrequency").value);
var resultDiv = document.getElementById("result");
if (isNaN(principal) || isNaN(annualRate) || isNaN(years) || isNaN(compoundingFrequency) || principal <= 0 || annualRate < 0 || years <= 0 || compoundingFrequency <= 0) {
resultDiv.innerHTML = "Please enter valid positive numbers for all fields.";
return;
}
var ratePerPeriod = annualRate / 100 / compoundingFrequency;
var numberOfPeriods = years * compoundingFrequency;
var futureValue = principal * Math.pow((1 + ratePerPeriod), numberOfPeriods);
var interestEarned = futureValue – principal;
resultDiv.innerHTML = "
Your Investment Growth
" +
"
Initial Investment: $" + principal.toFixed(2) + "" +
"
Annual Interest Rate: " + annualRate.toFixed(2) + "%" +
"
Compounding Frequency: " + compoundingFrequency + " times per year" +
"
Investment Period: " + years + " years" +
"
Total Value After " + years + " Years: $" + futureValue.toFixed(2) + "" +
"
Total Interest Earned: $" + interestEarned.toFixed(2) + "";
}
.calculator-container {
font-family: sans-serif;
max-width: 600px;
margin: 20px auto;
padding: 20px;
border: 1px solid #ccc;
border-radius: 8px;
background-color: #f9f9f9;
}
.calculator-form h2 {
text-align: center;
color: #333;
margin-bottom: 15px;
}
.calculator-form p {
text-align: center;
color: #555;
margin-bottom: 25px;
line-height: 1.6;
}
.form-group {
margin-bottom: 15px;
display: flex;
flex-direction: column;
}
.form-group label {
margin-bottom: 5px;
font-weight: bold;
color: #444;
}
.form-group input[type="number"],
.form-group select {
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 1em;
}
.form-group input[type="number"]::placeholder,
.form-group select::placeholder {
color: #aaa;
}
button {
display: block;
width: 100%;
padding: 12px 20px;
background-color: #007bff;
color: white;
border: none;
border-radius: 4px;
font-size: 1.1em;
cursor: pointer;
transition: background-color 0.3s ease;
margin-top: 20px;
}
button:hover {
background-color: #0056b3;
}
.calculator-result {
margin-top: 30px;
padding: 20px;
border: 1px solid #e0e0e0;
border-radius: 8px;
background-color: #fff;
text-align: left;
}
.calculator-result h3 {
color: #333;
margin-bottom: 15px;
text-align: center;
}
.calculator-result p {
margin-bottom: 10px;
color: #555;
line-height: 1.5;
}
.calculator-result strong {
font-weight: bold;
}