Changed around line 1
+ document.getElementById('portfolioForm').addEventListener('submit', function(e) {
+ e.preventDefault();
+
+ // Get input values
+ const currentAge = parseInt(document.getElementById('currentAge').value);
+ const retirementAge = parseInt(document.getElementById('retirementAge').value);
+ const currentPortfolio = parseFloat(document.getElementById('currentPortfolio').value);
+ const monthlyContribution = parseFloat(document.getElementById('monthlyContribution').value);
+ const expectedReturn = parseFloat(document.getElementById('expectedReturn').value) / 100;
+
+ // Validate inputs
+ if (retirementAge <= currentAge) {
+ alert('Retirement age must be greater than current age');
+ return;
+ }
+
+ // Calculate years until retirement
+ const yearsToRetirement = retirementAge - currentAge;
+
+ // Calculate future value
+ const monthlyRate = expectedReturn / 12;
+ const numberOfContributions = yearsToRetirement * 12;
+
+ // Future value calculation using compound interest formula
+ const futureValue = currentPortfolio * Math.pow(1 + expectedReturn, yearsToRetirement) +
+ monthlyContribution * (Math.pow(1 + monthlyRate, numberOfContributions) - 1) / monthlyRate;
+
+ // Calculate total contributions
+ const totalContributions = currentPortfolio + (monthlyContribution * numberOfContributions);
+
+ // Display results
+ document.getElementById('results').classList.remove('hidden');
+ document.getElementById('futureValue').textContent = formatCurrency(futureValue);
+ document.getElementById('yearsToRetirement').textContent = yearsToRetirement;
+ document.getElementById('totalContributions').textContent = formatCurrency(totalContributions);
+ });
+
+ function formatCurrency(value) {
+ return new Intl.NumberFormat('en-US', {
+ style: 'currency',
+ currency: 'USD',
+ maximumFractionDigits: 0
+ }).format(value);
+ }
+
+ // Input validation
+ document.querySelectorAll('input[type="number"]').forEach(input => {
+ input.addEventListener('input', function() {
+ if (this.value < 0) this.value = 0;
+ });
+ });