Compound Interest Calculator
Compound interest is the interest calculated on the initial principal, which also includes all of the accumulated interest from previous periods on a deposit or loan. It is the addition of interest to the principal sum of a loan or deposit, and the result is a new, higher principal. The next period's interest is then calculated on this new principal.
function calculateCompoundInterest() {
var principal = parseFloat(document.getElementById("principal").value);
var annualInterestRate = parseFloat(document.getElementById("annualInterestRate").value);
var compoundingFrequency = parseInt(document.getElementById("compoundingFrequency").value);
var timePeriod = parseFloat(document.getElementById("timePeriod").value);
var resultElement = document.getElementById("result");
if (isNaN(principal) || isNaN(annualInterestRate) || isNaN(compoundingFrequency) || isNaN(timePeriod) ||
principal <= 0 || annualInterestRate < 0 || compoundingFrequency <= 0 || timePeriod <= 0) {
resultElement.innerHTML = "Please enter valid positive numbers for all fields, with an interest rate of 0 or greater.";
return;
}
var ratePerPeriod = annualInterestRate / 100 / compoundingFrequency;
var numberOfPeriods = compoundingFrequency * timePeriod;
var futureValue = principal * Math.pow((1 + ratePerPeriod), numberOfPeriods);
var totalInterestEarned = futureValue – principal;
resultElement.innerHTML = "
Calculation Results:
" +
"
Future Value: $" + futureValue.toFixed(2) + "" +
"
Total Interest Earned: $" + totalInterestEarned.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-container h2 {
text-align: center;
color: #333;
margin-bottom: 20px;
}
.calculator-form {
display: grid;
grid-template-columns: 1fr;
gap: 15px;
}
.form-group {
display: flex;
flex-direction: column;
}
.form-group label {
margin-bottom: 5px;
font-weight: bold;
color: #555;
}
.form-group input[type="number"],
.form-group select {
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 16px;
}
.form-group button {
padding: 12px 20px;
background-color: #007bff;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
transition: background-color 0.3s ease;
}
.form-group button:hover {
background-color: #0056b3;
}
#result {
margin-top: 25px;
padding: 15px;
border-top: 1px solid #eee;
background-color: #fff;
border-radius: 4px;
}
#result h3 {
margin-top: 0;
color: #007bff;
}
#result p {
margin-bottom: 10px;
color: #333;
}