-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
112 lines (92 loc) · 3.79 KB
/
Copy pathscript.js
File metadata and controls
112 lines (92 loc) · 3.79 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
// DOM Core Selectors
const balance = document.getElementById('total-balance');
const income = document.getElementById('total-income');
const expense = document.getElementById('total-expense');
const list = document.getElementById('transaction-list');
const form = document.getElementById('exp-form');
const text = document.getElementById('text');
const amount = document.getElementById('amount');
const saveBtn = document.getElementById('save-state-btn');
// STATE CONTROLLER: Application always boots up with a clean zero baseline array
let transactions = [];
// Add New Transaction (Temporary Application State Only)
function addTransaction(e) {
e.preventDefault();
if (text.value.trim() === '' || amount.value.trim() === '') {
alert('Please enter a valid transaction description and amount');
return;
}
const transaction = {
id: generateID(),
text: text.value.trim(),
amount: +amount.value.trim() // Force conversion to numerical scale safely
};
transactions.push(transaction);
addTransactionDOM(transaction);
updateValues();
// Reset Form Input Layouts
text.value = '';
amount.value = '';
}
// Generate Unique Engine ID
function generateID() {
return Math.floor(Math.random() * 100000000);
}
// Render Core Elements inside DOM History Pipeline
function addTransactionDOM(transaction) {
const sign = transaction.amount < 0 ? '-' : '+';
const item = document.createElement('li');
// Dynamically allocate UI border flags
item.classList.add(transaction.amount < 0 ? 'minus' : 'plus');
item.innerHTML = `
${transaction.text} <span>${sign}₹${Math.abs(transaction.amount).toFixed(2)}</span>
<button class="delete-btn" onclick="removeTransaction(${transaction.id})">Delete</button>
`;
list.appendChild(item);
}
// Math Computation Core using functional Array operations (Map, Filter, Reduce)
function updateValues() {
const amounts = transactions.map(transaction => transaction.amount);
const total = amounts.reduce((acc, item) => (acc += item), 0).toFixed(2);
const totalIncome = amounts.filter(item => item > 0).reduce((acc, item) => (acc += item), 0).toFixed(2);
const totalExpense = (amounts.filter(item => item < 0).reduce((acc, item) => (acc += item), 0) * -1).toFixed(2);
balance.innerText = `₹${total}`;
income.innerText = `+ ₹${totalIncome}`;
expense.innerText = `- ₹${totalExpense}`;
}
// Remove Transaction from current staging list
function removeTransaction(id) {
transactions = transactions.filter(transaction => transaction.id !== id);
init();
}
// USER DRIVEN ACTION: LocalStorage is synced ONLY upon deliberate confirmation
function saveCurrentStatePermanently() {
if (transactions.length === 0) {
alert("Transaction ledger is empty! Please add entries before saving.");
return;
}
localStorage.setItem('transactions', JSON.stringify(transactions));
alert('✨ Ledger session successfully saved to system persistent cache!');
}
// Explicit startup checking loop to request manual restore validation
function checkSavedSession() {
const cachedData = JSON.parse(localStorage.getItem('transactions'));
if (cachedData && cachedData.length > 0) {
if (confirm("We recovered a permanently locked profile session. Would you like to restore it?")) {
transactions = cachedData;
init();
}
}
}
// Base System Loop Initializer
function init() {
list.innerHTML = '';
transactions.forEach(addTransactionDOM);
updateValues();
}
// Kickstart system rendering loop
init();
setTimeout(checkSavedSession, 400); // Clean scheduling trigger loop
// Operational Pipeline Listeners
form.addEventListener('submit', addTransaction);
saveBtn.addEventListener('click', saveCurrentStatePermanently);