-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
47 lines (33 loc) · 1.69 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
const totalBalance = document.getElementById('total-balance');
const totalIncome = document.getElementById('total-income');
const totalExpense = document.getElementById('total-expense');
const transactionsList = document.getElementById('transactions-list');
const transactionForm = document.getElementById('transaction-form');
let transactions = [];
transactionForm.addEventListener('submit', (e) => {
e.preventDefault(); // prevent the form from refreshing the page
const name = document.getElementById('name').value;
const amount = parseFloat(document.getElementById('amount').value); // convert amount to number
const date = document.getElementById('date').value;
const transaction = { name, amount, date };
transactions.push(transaction);
update();
transactionForm.reset();
});
function addTransaction(transaction) {
const li = document.createElement('li');
li.innerHTML = `<span>₹${transaction.amount.toFixed(0)}</span> <span>${transaction.name}</span> <span>${transaction.date}</span>`;
transactionsList.appendChild(li);
}
function update() {
const amounts = transactions.map(transaction => transaction.amount);
const income = amounts.filter(amount => amount > 0).reduce((acc, amount) => acc + amount, 0);
const expense = amounts.filter(amount => amount < 0).reduce((acc, amount) => acc + amount, 0);
const balance = income + expense;
totalBalance.innerText = `₹ ${balance.toFixed(0)}`;
totalIncome.innerText = `₹ ${income.toFixed(0)}`;
totalExpense.innerText = `₹ ${expense.toFixed(0)}`;
transactionsList.innerHTML = transactions.length ? '' : '<li>No transactions yet</li>';
transactions.forEach(addTransaction);
}
update();