Changed around line 1
+ let income = [];
+ let expenses = [];
+ let savingsGoal = 0;
+
+ function addIncome() {
+ const form = document.getElementById('income-form');
+ form.classList.toggle('hidden');
+ }
+
+ function addExpense() {
+ const form = document.getElementById('expense-form');
+ form.classList.toggle('hidden');
+ }
+
+ function setGoal() {
+ const goalInput = document.getElementById('goal');
+ savingsGoal = parseFloat(goalInput.value);
+ updateProgress();
+ }
+
+ function updateProgress() {
+ const totalIncome = income.reduce((sum, item) => sum + item.amount, 0);
+ const totalExpenses = expenses.reduce((sum, item) => sum + item.amount, 0);
+ const savings = totalIncome - totalExpenses;
+ const progress = (savings / savingsGoal) * 100;
+
+ const progressBar = document.querySelector('.progress');
+ progressBar.style.width = `${Math.min(progress, 100)}%`;
+ }
+
+ document.getElementById('income-form').addEventListener('submit', function(e) {
+ e.preventDefault();
+ const source = this.querySelector('input[type="text"]').value;
+ const amount = parseFloat(this.querySelector('input[type="number"]').value);
+ const frequency = this.querySelector('select').value;
+
+ income.push({ source, amount, frequency });
+ updateProgress();
+ this.reset();
+ this.classList.add('hidden');
+ });
+
+ document.getElementById('expense-form').addEventListener('submit', function(e) {
+ e.preventDefault();
+ const description = this.querySelector('input[type="text"]').value;
+ const amount = parseFloat(this.querySelector('input[type="number"]').value);
+ const category = this.querySelector('select').value;
+
+ expenses.push({ description, amount, category });
+ updateProgress();
+ this.reset();
+ this.classList.add('hidden');
+ });
+
+ // Chart.js initialization
+ const expenseCtx = document.getElementById('expenseChart').getContext('2d');
+ const savingsCtx = document.getElementById('savingsChart').getContext('2d');
+
+ const expenseChart = new Chart(expenseCtx, {
+ type: 'pie',
+ data: {
+ labels: ['Food', 'Transportation', 'Utilities', 'Other'],
+ datasets: [{
+ data: [0, 0, 0, 0],
+ backgroundColor: ['#FF6384', '#36A2EB', '#FFCE56', '#4BC0C0']
+ }]
+ }
+ });
+
+ const savingsChart = new Chart(savingsCtx, {
+ type: 'line',
+ data: {
+ labels: [],
+ datasets: [{
+ label: 'Savings',
+ data: [],
+ borderColor: '#4CAF50',
+ fill: false
+ }]
+ }
+ });
+
+ function updateCharts() {
+ const categories = ['Food', 'Transportation', 'Utilities', 'Other'];
+ const expenseData = categories.map(cat =>
+ expenses.filter(e => e.category === cat).reduce((sum, e) => sum + e.amount, 0)
+ );
+
+ expenseChart.data.datasets[0].data = expenseData;
+ expenseChart.update();
+
+ const totalIncome = income.reduce((sum, item) => sum + item.amount, 0);
+ const totalExpenses = expenses.reduce((sum, item) => sum + item.amount, 0);
+ const savings = totalIncome - totalExpenses;
+
+ savingsChart.data.labels.push(new Date().toLocaleDateString());
+ savingsChart.data.datasets[0].data.push(savings);
+ savingsChart.update();
+ }