High Yield Account Calculator

function calculateHighYieldGrowth() { var initialDeposit = parseFloat(document.getElementById('initialDeposit').value); var regularContribution = parseFloat(document.getElementById('regularContribution').value); var annualRate = parseFloat(document.getElementById('annualRate').value); var compoundingFrequency = parseInt(document.getElementById('compoundingFrequency').value); var investmentYears = parseFloat(document.getElementById('investmentYears').value); // Input validation if (isNaN(initialDeposit) || initialDeposit < 0) { alert('Please enter a valid initial deposit (non-negative number).'); return; } if (isNaN(regularContribution) || regularContribution < 0) { alert('Please enter a valid regular contribution (non-negative number).'); return; } if (isNaN(annualRate) || annualRate < 0) { alert('Please enter a valid annual interest rate (non-negative number).'); return; } if (isNaN(investmentYears) || investmentYears <= 0) { alert('Please enter a valid investment horizon (positive number of years).'); return; } var r_annual = annualRate / 100; // Convert percentage to decimal var i = r_annual / compoundingFrequency; // Interest rate per compounding period var N = investmentYears * compoundingFrequency; // Total number of compounding periods var totalFutureValue = 0; var totalPrincipalContributed = 0; var totalInterestEarned = 0; // Handle zero interest rate separately to avoid division by zero if (i === 0) { totalFutureValue = initialDeposit + (regularContribution * N); totalPrincipalContributed = initialDeposit + (regularContribution * N); totalInterestEarned = 0; } else { // Future Value of Initial Deposit var fv_initial = initialDeposit * Math.pow((1 + i), N); // Future Value of Contributions (Annuity Ordinary) // Assumes contributions are made at the end of each compounding period var fv_contributions = regularContribution * (Math.pow((1 + i), N) – 1) / i; totalFutureValue = fv_initial + fv_contributions; totalPrincipalContributed = initialDeposit + (regularContribution * N); totalInterestEarned = totalFutureValue – totalPrincipalContributed; } document.getElementById('totalFutureValue').innerText = '$' + totalFutureValue.toFixed(2); document.getElementById('totalPrincipalContributed').innerText = '$' + totalPrincipalContributed.toFixed(2); document.getElementById('totalInterestEarned').innerText = '$' + totalInterestEarned.toFixed(2); document.getElementById('result').style.display = 'block'; }

Leave a Reply

Your email address will not be published. Required fields are marked *