This repository has been archived by the owner on Dec 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
148 lines (118 loc) · 4.61 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Retrieve transactions from local storage or initialize an empty array
let transactions = JSON.parse(localStorage.getItem('transactions')) || [];
// Display transactions on page load
displayTransactions();
// Calculate and display the current balance, incomes, and expenses
calculateBalance();
displayIncomes();
displayExpenses();
// Add transaction event listener
document.getElementById('transactionForm').addEventListener('submit', addTransaction);
// Add transaction
function addTransaction(e) {
e.preventDefault();
// Get user inputs
const description = document.getElementById('description').value;
const amount = parseFloat(document.getElementById('amount').value);
const type = document.getElementById('type').value;
// Get current date and time
const timestamp = new Date();
// Create a new transaction object
const transaction = {
id: Date.now(),
description,
amount,
type,
timestamp
};
// Add the transaction to the array
transactions.push(transaction);
// Update local storage
updateLocalStorage();
// Clear form inputs
document.getElementById('description').value = '';
document.getElementById('amount').value = '';
// Display the updated transactions, incomes, and expenses
displayTransactions();
displayIncomes();
displayExpenses();
// Recalculate and display the current balance
calculateBalance();
}
// Display transactions
function displayTransactions() {
const transactionList = document.getElementById('transactionList');
// Clear the transaction list
transactionList.innerHTML = '';
// Reverse the transactions array to display the most recent transaction first
const reversedTransactions = transactions.slice().reverse();
// Iterate through reversed transactions and create list items
reversedTransactions.forEach(transaction => {
const listItem = document.createElement('li');
const dateTime = new Date(transaction.timestamp).toLocaleString();
listItem.innerHTML = `
<span class="ty">${transaction.description}</span>
<span class="${transaction.type}">₹${transaction.amount.toFixed(2)}</span>
<span class="timestamp">${dateTime}</span>
<button onclick="deleteTransaction(${transaction.id})">Delete</button>
`;
transactionList.appendChild(listItem);
});
}
// Display incomes
function displayIncomes() {
const incomeList = document.getElementById('incomeList');
// Clear the income list
incomeList.innerHTML = '';
// Filter transactions by income type
const incomes = transactions.filter(transaction => transaction.type === 'income');
// Iterate through incomes and create list items
incomes.forEach(income => {
const listItem = document.createElement('div');
listItem.textContent = `${income.description}: ₹${income.amount.toFixed(2)}`;
incomeList.appendChild(listItem);
});
}
// Display expenses
function displayExpenses() {
const expenseList = document.getElementById('expenseList');
// Clear the expense list
expenseList.innerHTML = '';
// Filter transactions by expense type
const expenses = transactions.filter(transaction => transaction.type === 'expense');
// Iterate through expenses and create list items
expenses.forEach(expense => {
const listItem = document.createElement('div');
listItem.textContent = `${expense.description}: ₹${expense.amount.toFixed(2)}`;
expenseList.appendChild(listItem);
});
}
// Delete transaction
function deleteTransaction(id) {
// Remove the transaction from the array
transactions = transactions.filter(transaction => transaction.id !== id);
// Update local storage
updateLocalStorage();
// Display the updated transactions, incomes, and expenses
displayTransactions();
displayIncomes();
displayExpenses();
// Recalculate and display the current balance
calculateBalance();
}
// Calculate and display the current balance
function calculateBalance() {
const balance = document.getElementById('balance');
const income = transactions
.filter(transaction => transaction.type === 'income')
.reduce((total, transaction) => total + transaction.amount, 0);
const expense = transactions
.filter(transaction => transaction.type === 'expense')
.reduce((total, transaction) => total + transaction.amount, 0);
const currentBalance = income - expense;
balance.textContent = `Current Balance: ₹${currentBalance.toFixed(2)}`;
}
// Update local storage
function updateLocalStorage() {
localStorage.setItem('transactions', JSON.stringify(transactions));
}