function InvestmentCalculator() {
const [mode, setMode] = useState('simple');
const [principal, setPrincipal] = useState('');
const [rate, setRate] = useState('');
const [years, setYears] = useState('');
const [periods, setPeriods] = useState([{ year: 1, amount: '', rate: '' }]);
const [result, setResult] = useState(null);
const calculateSimple = () => {
if (principal && rate && years) {
const p = parseFloat(principal);
const r = parseFloat(rate);
const y = parseFloat(years);
const amount = p * Math.pow((1 + r / 100), y);
const interest = amount - p;
setResult({ type: 'simple', principal: p, totalAmount: amount, totalInterest: interest, rate: r, years: y });
}
};
const calculateAdvanced = () => {
let currentAmount = 0;
let totalInvested = 0;
let breakdown = [];
periods.forEach((period, index) => {
const amt = parseFloat(period.amount) || 0;
const r = parseFloat(period.rate) || 0;
if (index === 0) {
currentAmount = amt;
totalInvested = amt;
} else {
const prevRate = parseFloat(periods[index - 1].rate) || 0;
currentAmount = currentAmount * (1 + prevRate / 100) + amt;
totalInvested += amt;
}
const grownAmount = currentAmount * (1 + r / 100);
const interest = grownAmount - currentAmount;
breakdown.push({ year: period.year, invested: amt, rate: r, startAmount: currentAmount, endAmount: grownAmount, interest });
currentAmount = grownAmount;
});
setResult({ type: 'advanced', totalInvested, totalAmount: currentAmount, totalInterest: currentAmount - totalInvested, breakdown });
};
const addPeriod = () => setPeriods([...periods, { year: periods.length + 1, amount: '', rate: '' }]);
const removePeriod = (index) => {
if (periods.length > 1) {
const newPeriods = periods.filter((_, i) => i !== index).map((p, i) => ({ ...p, year: i + 1 }));
setPeriods(newPeriods);
}
};
const updatePeriod = (index, field, value) => {
const newPeriods = [...periods];
newPeriods[index][field] = value;
setPeriods(newPeriods);
};
return (
💹 Advanced Investment Calculator
Calculate returns with variable interest rates
{mode === 'simple' ? (
) : (
How it works: Add different investment amounts and rates for each year. Previous year's amount compounds and new investments are added.
{periods.map((period, index) => (
Year {period.year}
{periods.length > 1 && (
)}
))}
)}
{result && result.type === 'simple' && (
Principal Amount
₹{result.principal.toLocaleString('en-IN', { maximumFractionDigits: 2 })}
Total Interest Earned
₹{result.totalInterest.toLocaleString('en-IN', { maximumFractionDigits: 2 })}
Maturity Amount
₹{result.totalAmount.toLocaleString('en-IN', { maximumFractionDigits: 2 })}
At {result.rate}% p.a. for {result.years} years
)}
{result && result.type === 'advanced' && (
Total Invested
₹{result.totalInvested.toLocaleString('en-IN', { maximumFractionDigits: 2 })}
Total Interest Earned
₹{result.totalInterest.toLocaleString('en-IN', { maximumFractionDigits: 2 })}
Final Amount
₹{result.totalAmount.toLocaleString('en-IN', { maximumFractionDigits: 2 })}
Year-by-Year Breakdown
{result.breakdown.map((item, index) => (
Year {item.year} @ {item.rate}%
{item.invested > 0 &&
New Investment: ₹{item.invested.toLocaleString('en-IN')}
}
Start: ₹{item.startAmount.toLocaleString('en-IN', { maximumFractionDigits: 0 })}
Interest: +₹{item.interest.toLocaleString('en-IN', { maximumFractionDigits: 0 })}
End: ₹{item.endAmount.toLocaleString('en-IN', { maximumFractionDigits: 0 })}
))}
)}
);
}
// Export to global registry
window.InvestmentCalculator = InvestmentCalculator;