-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
71 lines (60 loc) · 2.04 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
document.getElementById('expForm').addEventListener('submit', addTransaction);
const transactions = JSON.parse(localStorage.getItem('transactions')) || [];
function addTransaction(e) {
e.preventDefault();
let type = document.getElementById('type').value;
let category = document.getElementById('category').value;
let amount = document.getElementById('amount').value;
if (type != 'chooseOne'
&& category.length > 0
&& amount > 0) {
const transaction = {
type,
category,
amount,
id: transactions.length > 0 ? transactions[transactions.length - 1].id + 1 : 1,
}
transactions.push(transaction);
localStorage.setItem('transactions', JSON.stringify(transactions));
}
document.getElementById('expForm').reset();
showTransactions();
updateBalance();
}
const showTransactions = () => {
const transactionTable = document.getElementById('transactionTable');
transactionTable.innerHTML = '';
for (let i = 0; i < transactions.length; i++) {
transactionTable.innerHTML += `
<tr>
<td>${transactions[i].type}</td>
<td>${transactions[i].category}</td>
<td>$${transactions[i].amount}</td>
<td><a class="deleteButton" onclick="deleteTransaction(${transactions[i].id})">
Delete</td>
</tr>
`;
}
}
const deleteTransaction = (id) => {
for (let i = 0; i < transactions.length; i++) {
if (transactions[i].id == id) {
transactions.splice(i, 1);
}
}
// localStorage
localStorage.setItem('transactions', JSON.stringify(transactions));
showTransactions();
updateBalance();
}
const updateBalance = () => {
let balance = 0;
transactions.forEach((transaction) => {
if (transaction.type === "income") {
balance += Number(transaction.amount);
} else if (transaction.type === "expenses") {
balance -= Number(transaction.amount);
}
});
document.querySelector(".balance").textContent = balance;
}