-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
94 lines (83 loc) · 3.96 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
const submitButton = document.getElementById('submit-transaction');
submitButton.addEventListener('click', submitData);
const clearButton = document.getElementById('clear');
clearButton.addEventListener('click', clearData);
function submitData (event) {
console.log('clicked ok');
var balance = document.getElementById('balance-number');
var totalIncome = document.getElementById('income-number');
var totalExpense = document.getElementById('expense-number');
var transactionName = document.getElementById('transaction-name');
var transactionNumber = document.getElementById('transaction-number');
var errorMsg = document.getElementById('error');
var list = document.getElementById('list');
var node = document.createElement('li');
var nodeText = document.createTextNode(transactionName.value);
var nodeNumber = document.createTextNode(transactionNumber.value);
var span = document.createElement('span');
var delBtn = document.createElement('button');
if ((transactionName.value == null || transactionName.value == undefined || transactionName.value.trim() == "") ||
(transactionNumber.value == null || transactionNumber.value == undefined || transactionNumber.value.trim() == "")) {
console.log('if OK');
if(submitButton.clicked === false) {
return false;
} else {
errorMsg.style.visibility = "visible";
}
} else {
console.log('else ok', transactionNumber.value);
errorMsg.style.visibility = "hidden";
delBtn.innerText = "X";
delBtn.classList.add('delete-btn');
node.appendChild(nodeText);
node.appendChild(delBtn);
span.appendChild(nodeNumber);
if (span.innerHTML > 0){
span.style.borderRight = "5px solid green";
} else {
span.style.borderRight = "5px solid red";
}
node.appendChild(span);
list.appendChild(node);
let total = 0;
let plusIncome = 0;
let minusExpense = 0;
let transactionList = document.querySelectorAll('#list span');
transactionList.forEach(function(item, index) {
let valueItem = parseInt(item.innerHTML);
total += valueItem;
if (valueItem >= 0) {
plusIncome += valueItem;
} else {
minusExpense += valueItem;
}
totalIncome.innerHTML = plusIncome;
totalExpense.innerHTML = minusExpense;
if (total >= 0) {
balance.innerHTML = total;
balance.style.color = "green";
} else {
balance.innerHTML = total;
balance.style.color = "red";
}
});
}
delBtn.addEventListener('click', function(e){
let allTransactions = document.querySelectorAll('#list span');
if (span.innerHTML > 0) {
totalIncome.innerHTML = totalIncome.innerHTML - span.innerHTML;
} else {
totalExpense.innerHTML = totalExpense.innerHTML - span.innerHTML;
}
balance.innerHTML = totalIncome.innerHTML - Math.abs(totalExpense.innerHTML);
node.remove();
});
}
function clearData() {
list.innerHTML = "";
document.getElementById('transaction-name').value = '';
document.getElementById('transaction-number').value = '';
document.getElementById('income-number').innerHTML = '0.00';
document.getElementById('expense-number').innerHTML = '0.00';
document.getElementById('balance-number').innerHTML = '0.00';
}