forked from sumn2u/learn-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.mjs
More file actions
112 lines (91 loc) · 2.84 KB
/
index.mjs
File metadata and controls
112 lines (91 loc) · 2.84 KB
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
// Select elements
const balanceEl = document.getElementById("balance");
const incomeEl = document.getElementById("income");
const expenseEl = document.getElementById("expense");
const listEl = document.getElementById("transaction-list");
const form = document.getElementById("transaction-form");
const textInput = document.getElementById("text");
const amountInput = document.getElementById("amount");
// Load transactions from localStorage
let transactions = JSON.parse(localStorage.getItem("transactions")) || [];
// Add transaction
function addTransaction(e) {
e.preventDefault();
const text = textInput.value.trim();
const amount = +amountInput.value;
if (text === "" || amount === 0 || isNaN(amount)) {
alert("Please enter valid description and amount.");
return;
}
const transaction = {
id: Date.now(),
text,
amount,
};
transactions.push(transaction);
updateLocalStorage();
renderTransactions();
textInput.value = "";
amountInput.value = "";
}
// Delete transaction
function deleteTransaction(id) {
transactions = transactions.filter((t) => t.id !== id);
updateLocalStorage();
renderTransactions();
}
// Edit transaction
function editTransaction(id) {
const t = transactions.find((t) => t.id === id);
if (!t) return;
const newText = prompt("Edit description:", t.text);
const newAmount = parseFloat(prompt("Edit amount:", t.amount));
if (newText && !isNaN(newAmount)) {
t.text = newText;
t.amount = newAmount;
updateLocalStorage();
renderTransactions();
}
}
// Update totals
function updateSummary() {
const amounts = transactions.map((t) => t.amount);
const total = amounts.reduce((a, b) => a + b, 0).toFixed(2);
const income = amounts
.filter((a) => a > 0)
.reduce((a, b) => a + b, 0)
.toFixed(2);
const expense = (
amounts.filter((a) => a < 0).reduce((a, b) => a + b, 0) * -1
).toFixed(2);
balanceEl.textContent = `₹${total}`;
incomeEl.textContent = `₹${income}`;
expenseEl.textContent = `₹${expense}`;
}
// Render transactions
function renderTransactions() {
listEl.innerHTML = "";
transactions.forEach((t) => {
const li = document.createElement("li");
li.classList.add(t.amount < 0 ? "expense" : "income");
li.innerHTML = `
${t.text} <span>₹${t.amount}</span>
<div>
<button onclick="editTransaction(${t.id})">✏️</button>
<button onclick="deleteTransaction(${t.id})">❌</button>
</div>
`;
listEl.appendChild(li);
});
updateSummary();
}
// Save to localStorage
function updateLocalStorage() {
localStorage.setItem("transactions", JSON.stringify(transactions));
}
// Initialize
renderTransactions();
form.addEventListener("submit", addTransaction);
// Expose functions globally (for inline onclick)
window.editTransaction = editTransaction;
window.deleteTransaction = deleteTransaction;