Changed around line 1
+ document.addEventListener('DOMContentLoaded', () => {
+ const incomeForm = document.getElementById('incomeForm');
+ const incomeList = document.getElementById('incomeList');
+ let incomes = JSON.parse(localStorage.getItem('incomes')) || [];
+
+ function updateSummary() {
+ const total = incomes.reduce((sum, income) => sum + parseFloat(income.amount), 0);
+ document.getElementById('totalIncome').textContent = formatCurrency(total);
+
+ const currentMonth = new Date().getMonth();
+ const monthlyTotal = incomes
+ .filter(income => new Date(income.date).getMonth() === currentMonth)
+ .reduce((sum, income) => sum + parseFloat(income.amount), 0);
+ document.getElementById('monthlyIncome').textContent = formatCurrency(monthlyTotal);
+
+ const average = incomes.length ? total / incomes.length : 0;
+ document.getElementById('averageIncome').textContent = formatCurrency(average);
+ }
+
+ function formatCurrency(amount) {
+ return new Intl.NumberFormat('en-US', {
+ style: 'currency',
+ currency: 'USD'
+ }).format(amount);
+ }
+
+ function formatDate(dateString) {
+ return new Date(dateString).toLocaleDateString('en-US', {
+ year: 'numeric',
+ month: 'short',
+ day: 'numeric'
+ });
+ }
+
+ function addIncome(e) {
+ e.preventDefault();
+
+ const amount = document.getElementById('amount').value;
+ const source = document.getElementById('source').value;
+ const date = document.getElementById('date').value;
+
+ const newIncome = {
+ id: Date.now(),
+ amount,
+ source,
+ date
+ };
+
+ incomes.push(newIncome);
+ localStorage.setItem('incomes', JSON.stringify(incomes));
+
+ displayIncomes();
+ updateSummary();
+ incomeForm.reset();
+
+ // Animation for success
+ const button = e.target.querySelector('button');
+ button.style.backgroundColor = '#2ecc71';
+ setTimeout(() => {
+ button.style.backgroundColor = '';
+ }, 1000);
+ }
+
+ function deleteIncome(id) {
+ incomes = incomes.filter(income => income.id !== id);
+ localStorage.setItem('incomes', JSON.stringify(incomes));
+ displayIncomes();
+ updateSummary();
+ }
+
+ function displayIncomes() {
+ incomeList.innerHTML = '';
+
+ incomes
+ .sort((a, b) => new Date(b.date) - new Date(a.date))
+ .forEach(income => {
+ const row = document.createElement('tr');
+ row.innerHTML = `
+
${formatDate(income.date)} | +
${income.source} | +
${formatCurrency(income.amount)} | + `;
+ incomeList.appendChild(row);
+ });
+ }
+
+ incomeForm.addEventListener('submit', addIncome);
+ window.deleteIncome = deleteIncome;
+
+ // Initial display
+ displayIncomes();
+ updateSummary();
+ });