-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
78 lines (68 loc) · 2.49 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
const amount = document.querySelector('.amount');
const income = document.querySelector('.income p');
const expense = document.querySelector('.expense p');
const historyList = document.querySelector('.history-list');
const text = document.getElementById('text');
const number = document.getElementById('number');
const form = document.getElementById('form');
const closeBtn = document.querySelectorAll('.close-btn');
let totalBalance = 0;
let totalIncome = 0;
let totalExpense = 0;
const addToHistory = (reason, kharcha) => {
const item = document.createElement('div');
item.classList.add('hist');
if (kharcha < 0) {
sign = '-';
item.classList.add('red');
} else if (kharcha > 0) {
sign = '+';
item.classList.add('green');
}
item.innerHTML = `
<h2 class="close-btn">✗</h2>
<h2>${reason}</h2>
<h3 class="amount">${sign}${Math.abs(kharcha)}</h3>
`
historyList.appendChild(item);
}
const setValues = () => {
amount.innerText = '₹' + Number(totalBalance.toString().replace(/^0+/, '')).toFixed(2);
income.innerText = '₹' + Number(totalIncome.toString().replace(/^0+/, '')).toFixed(2);
expense.innerText = '₹' + Math.abs(Number(totalExpense.toString().replace(/^0+/, ''))).toFixed(2);
}
form.addEventListener('submit', (e) => {
e.preventDefault();
if (number.value === '' && text.value === '') {
alert("Enter the credentials!");
} else if (number.value === '') {
alert("Enter the amount!");
} else if (text.value === '') {
alert("Enter the reason for transaction!");
} else {
totalBalance += parseFloat(number.value);
if (number.value > 0) {
totalIncome += parseFloat(number.value);
} else {
totalExpense += parseFloat(number.value);
}
setValues();
addToHistory(text.value, number.value);
text.value = '';
number.value = '';
}
})
historyList.addEventListener('click', (event) => {
if (event.target && event.target.className == 'close-btn') {
const parent = event.target.parentNode;
parent.remove();
histNum = parseFloat(parent.childNodes[5].innerText);
totalBalance = parseFloat(amount.innerText.slice(1)) - histNum;
if (histNum < 0) {
totalExpense = histNum + parseFloat(expense.innerText.slice(1));
} else if (histNum > 0) {
totalIncome = parseFloat(income.innerText.slice(1)) - histNum;
}
setValues();
}
})