-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscript.js
81 lines (65 loc) · 2.55 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
const expenseForm = document.getElementById('expense-form');
const expenseInput = document.getElementById('expense-input');
const amountInput = document.getElementById('amount-input');
const categoryInput = document.getElementById('category-input');
const transactionList = document.getElementById('transaction-list');
const totalExpense = document.getElementById('total-expense');
const totalIncome = document.getElementById('total-income');
const balance = document.getElementById('balance');
expenseForm.addEventListener('submit', function(event) {
event.preventDefault();
const description = expenseInput.value.trim();
const amount = parseFloat(amountInput.value.trim());
const category = categoryInput.value;
if (description === '' || isNaN(amount) || amount <= 0) {
alert('Please enter a valid expense description and amount.');
return;
}
addTransaction(description, amount, category);
updateSummary();
clearInputs();
});
function addTransaction(description, amount, category) {
const transactionRow = document.createElement('tr');
transactionRow.innerHTML = `
<td>${description}</td>
<td>${category}</td>
<td>${amount.toFixed(2)}</td>
<td><button class="delete-btn">Delete</button></td>
`;
transactionList.appendChild(transactionRow);
transactionRow.querySelector('.delete-btn').addEventListener('click', function() {
transactionRow.remove();
updateSummary();
});
}
function updateSummary() {
let totalExpenses = 0;
let totalIncomes = 0;
const transactions = transactionList.querySelectorAll('tr');
transactions.forEach(function(transaction) {
const amount = parseFloat(transaction.children[2].textContent);
const category = transaction.children[1].textContent;
if (category === 'Income') {
totalIncomes += amount;
} else {
totalExpenses += amount;
}
});
totalExpense.textContent = totalExpenses.toFixed(2);
totalIncome.textContent = totalIncomes.toFixed(2);
balance.textContent = (totalIncomes - totalExpenses).toFixed(2);
}
function clearInputs() {
expenseInput.value = '';
amountInput.value = '';
categoryInput.value = 'Expense';
}
function showNotification(message) {
const notification = document.getElementById('notification');
notification.textContent = message;
notification.classList.remove('hidden');
setTimeout(function() {
notification.classList.add('hidden');
}, 2000); // Notification will disappear after 2 seconds
}