|
| 1 | +// Select elements |
| 2 | +const balanceEl = document.getElementById("balance"); |
| 3 | +const incomeEl = document.getElementById("income"); |
| 4 | +const expenseEl = document.getElementById("expense"); |
| 5 | +const listEl = document.getElementById("transaction-list"); |
| 6 | +const form = document.getElementById("transaction-form"); |
| 7 | +const textInput = document.getElementById("text"); |
| 8 | +const amountInput = document.getElementById("amount"); |
| 9 | + |
| 10 | +// Load transactions from localStorage |
| 11 | +let transactions = JSON.parse(localStorage.getItem("transactions")) || []; |
| 12 | + |
| 13 | +// Add transaction |
| 14 | +function addTransaction(e) { |
| 15 | + e.preventDefault(); |
| 16 | + |
| 17 | + const text = textInput.value.trim(); |
| 18 | + const amount = +amountInput.value; |
| 19 | + |
| 20 | + if (text === "" || amount === 0 || isNaN(amount)) { |
| 21 | + alert("Please enter valid description and amount."); |
| 22 | + return; |
| 23 | + } |
| 24 | + |
| 25 | + const transaction = { |
| 26 | + id: Date.now(), |
| 27 | + text, |
| 28 | + amount, |
| 29 | + }; |
| 30 | + |
| 31 | + transactions.push(transaction); |
| 32 | + updateLocalStorage(); |
| 33 | + renderTransactions(); |
| 34 | + |
| 35 | + textInput.value = ""; |
| 36 | + amountInput.value = ""; |
| 37 | +} |
| 38 | + |
| 39 | +// Delete transaction |
| 40 | +function deleteTransaction(id) { |
| 41 | + transactions = transactions.filter((t) => t.id !== id); |
| 42 | + updateLocalStorage(); |
| 43 | + renderTransactions(); |
| 44 | +} |
| 45 | + |
| 46 | +// Edit transaction |
| 47 | +function editTransaction(id) { |
| 48 | + const t = transactions.find((t) => t.id === id); |
| 49 | + if (!t) return; |
| 50 | + |
| 51 | + const newText = prompt("Edit description:", t.text); |
| 52 | + const newAmount = parseFloat(prompt("Edit amount:", t.amount)); |
| 53 | + |
| 54 | + if (newText && !isNaN(newAmount)) { |
| 55 | + t.text = newText; |
| 56 | + t.amount = newAmount; |
| 57 | + updateLocalStorage(); |
| 58 | + renderTransactions(); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +// Update totals |
| 63 | +function updateSummary() { |
| 64 | + const amounts = transactions.map((t) => t.amount); |
| 65 | + const total = amounts.reduce((a, b) => a + b, 0).toFixed(2); |
| 66 | + const income = amounts |
| 67 | + .filter((a) => a > 0) |
| 68 | + .reduce((a, b) => a + b, 0) |
| 69 | + .toFixed(2); |
| 70 | + const expense = ( |
| 71 | + amounts.filter((a) => a < 0).reduce((a, b) => a + b, 0) * -1 |
| 72 | + ).toFixed(2); |
| 73 | + |
| 74 | + balanceEl.textContent = `₹${total}`; |
| 75 | + incomeEl.textContent = `₹${income}`; |
| 76 | + expenseEl.textContent = `₹${expense}`; |
| 77 | +} |
| 78 | + |
| 79 | +// Render transactions |
| 80 | +function renderTransactions() { |
| 81 | + listEl.innerHTML = ""; |
| 82 | + |
| 83 | + transactions.forEach((t) => { |
| 84 | + const li = document.createElement("li"); |
| 85 | + li.classList.add(t.amount < 0 ? "expense" : "income"); |
| 86 | + |
| 87 | + li.innerHTML = ` |
| 88 | + ${t.text} <span>₹${t.amount}</span> |
| 89 | + <div> |
| 90 | + <button onclick="editTransaction(${t.id})">✏️</button> |
| 91 | + <button onclick="deleteTransaction(${t.id})">❌</button> |
| 92 | + </div> |
| 93 | + `; |
| 94 | + |
| 95 | + listEl.appendChild(li); |
| 96 | + }); |
| 97 | + |
| 98 | + updateSummary(); |
| 99 | +} |
| 100 | + |
| 101 | +// Save to localStorage |
| 102 | +function updateLocalStorage() { |
| 103 | + localStorage.setItem("transactions", JSON.stringify(transactions)); |
| 104 | +} |
| 105 | + |
| 106 | +// Initialize |
| 107 | +renderTransactions(); |
| 108 | +form.addEventListener("submit", addTransaction); |
| 109 | + |
| 110 | +// Expose functions globally (for inline onclick) |
| 111 | +window.editTransaction = editTransaction; |
| 112 | +window.deleteTransaction = deleteTransaction; |
0 commit comments