Interest Calculator
Results
Principal Amount:
$0.00
Total Interest Earned:
$0.00
Final Amount:
$0.00
Calculation Type:
Simple Interest
document.addEventListener(‘DOMContentLoaded’, function() {
const principalInput = document.getElementById(‘principal’);
const rateInput = document.getElementById(‘rate’);
const timeValueInput = document.getElementById(‘time-value’);
const timeUnitSelect = document.getElementById(‘time-unit’);
const compoundSelect = document.getElementById(‘compound-frequency’);
const calculateBtn = document.getElementById(‘calculate-btn’);
const resetBtn = document.getElementById(‘reset-btn’);
const resultsContainer = document.getElementById(‘results’);
// Result elements
const resultPrincipal = document.getElementById(‘result-principal’);
const resultInterest = document.getElementById(‘result-interest’);
const resultTotal = document.getElementById(‘result-total’);
const resultType = document.getElementById(‘result-type’);
// Calculate on button click
calculateBtn.addEventListener(‘click’, calculateInterest);
// Reset form
resetBtn.addEventListener(‘click’, resetCalculator);
function calculateInterest() {
const principal = parseFloat(principalInput.value);
const rate = parseFloat(rateInput.value);
const timeValue = parseFloat(timeValueInput.value);
const timeUnit = timeUnitSelect.value;
const compoundFreq = parseInt(compoundSelect.value);
// Validate inputs
if (isNaN(principal) || principal <= 0) {
alert("Please enter a valid principal amount.");
return;
}
if (isNaN(rate) || rate <= 0) {
alert("Please enter a valid interest rate.");
return;
}
if (isNaN(timeValue) || timeValue <= 0) {
alert("Please enter a valid time period.");
return;
}
// Convert time to years
let timeInYears = timeUnit === 'months' ? timeValue / 12 : timeValue;
let totalAmount, interest, calculationType;
if (compoundFreq === 0) {
// Simple Interest Calculation
calculationType = "Simple Interest";
interest = principal * (rate / 100) * timeInYears;
totalAmount = principal + interest;
} else {
// Compound Interest Calculation
const n = compoundFreq;
totalAmount = principal * Math.pow(1 + (rate / 100) / n, n * timeInYears);
interest = totalAmount - principal;
// Set compounding type text
switch(compoundFreq) {
case 1: calculationType = "Annually Compounded"; break;
case 2: calculationType = "Semi-Annually Compounded"; break;
case 4: calculationType = "Quarterly Compounded"; break;
case 12: calculationType = "Monthly Compounded"; break;
case 365: calculationType = "Daily Compounded"; break;
}
}
// Display results
resultPrincipal.textContent = formatCurrency(principal);
resultInterest.textContent = formatCurrency(interest);
resultTotal.textContent = formatCurrency(totalAmount);
resultType.textContent = calculationType;
// Show results
resultsContainer.style.display = 'block';
}
function resetCalculator() {
principalInput.value = '';
rateInput.value = '';
timeValueInput.value = '';
timeUnitSelect.value = 'years';
compoundSelect.value = '1';
resultsContainer.style.display = 'none';
}
function formatCurrency(amount) {
return '$' + amount.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,');
}
});
function interest_calculator_shortcode() {
ob_start();
?>