Results
Your future investment value will appear here.
.calculator-wrapper {
font-family: sans-serif;
max-width: 600px;
margin: 20px auto;
border: 1px solid #ccc;
border-radius: 8px;
overflow: hidden;
display: flex;
flex-wrap: wrap;
}
.calculator-form {
flex: 1;
padding: 20px;
background-color: #f9f9f9;
}
.calculator-result {
flex: 1;
padding: 20px;
background-color: #e9ecef;
text-align: center;
}
.form-group {
margin-bottom: 15px;
}
.form-group label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}
.form-group input[type="number"],
.form-group select {
width: calc(100% – 12px);
padding: 8px;
border: 1px solid #ccc;
border-radius: 4px;
}
.calculator-form button {
background-color: #007bff;
color: white;
padding: 10px 15px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
margin-top: 10px;
}
.calculator-form button:hover {
background-color: #0056b3;
}
#result {
font-size: 1.2em;
margin-top: 20px;
color: #333;
}
h2, h3 {
text-align: center;
color: #333;
margin-bottom: 20px;
}
function calculateCompoundInterest() {
var principal = parseFloat(document.getElementById("principal").value);
var annualInterestRate = parseFloat(document.getElementById("interestRate").value);
var years = parseFloat(document.getElementById("years").value);
var compoundingFrequency = parseInt(document.getElementById("compoundingFrequency").value);
var resultElement = document.getElementById("result");
if (isNaN(principal) || isNaN(annualInterestRate) || isNaN(years) || isNaN(compoundingFrequency)) {
resultElement.innerHTML = "Please enter valid numbers for all fields.";
return;
}
if (principal <= 0 || annualInterestRate < 0 || years <= 0 || compoundingFrequency <= 0) {
resultElement.innerHTML = "Please enter positive values for principal, years, and a non-negative interest rate.";
return;
}
var ratePerPeriod = (annualInterestRate / 100) / compoundingFrequency;
var numberOfPeriods = years * compoundingFrequency;
var futureValue = principal * Math.pow((1 + ratePerPeriod), numberOfPeriods);
resultElement.innerHTML = "Your investment will be worth:
$" + futureValue.toFixed(2) + " after " + years + " years.";
}