-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
executable file
·121 lines (100 loc) · 2.62 KB
/
Copy pathscript.js
File metadata and controls
executable file
·121 lines (100 loc) · 2.62 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
113
114
115
116
117
118
119
120
121
let detail;
let amount;
let dispArr;
// holds all transactions
let transactions = [];
// get values from inputs
function getInputValues() {
detail = document.getElementById("detail").value;
amount = Number(document.getElementById("amount").value);
}
// validate inputs
function isValid() {
if (!detail || !amount) {
alert("Detail and Amount are both required");
return false;
}
return true;
}
// populate the table
function displayTable() {
const tableBody = document.getElementById("tableBody");
tableBody.innerHTML = "";
if(transactions != null)
{
for (let i = 0; i < transactions.length; i++) {
tableBody.innerHTML += `
<tr>
<th>${transactions[i].type}</th>
<th>${transactions[i].detail}</th>
<th>${transactions[i].amount}</th>
</tr>
`;
}
}
else
{
retrieveLocal();
if(dispArr != null)
{
for (let i = 0; i < dispArr.length; i++) {
tableBody.innerHTML += `
<tr>
<th>${dispArr[i].type}</th>
<th>${dispArr[i].detail}</th>
<th>${dispArr[i].amount}</th>
</tr>
`;
}
}
else
{
alert("Display array retrieved nothing");
}
}
}
function getResults () {
const incomeTotal = document.getElementById("incomeTotal");
const expenseTotal = document.getElementById("expenseTotal");
const amountTotal = document.getElementById("amountTotal");
let income = 0;
let expense = 0;
let amount = 0;
for (let i = 0; i < transactions.length; i++) {
if (transactions[i].type === "Income") {
income += transactions[i].amount;
}
if (transactions[i].type === "Expense") {
expense += transactions[i].amount;
}
}
incomeTotal.innerHTML = income;
expenseTotal.innerHTML = expense;
amountTotal.innerHTML = income - expense;
sendLocal();
}
function calc(type) {
getInputValues();
if (!isValid()) return;
transactions.push({ type, detail, amount })
getResults();
displayTable();
clearInoutsFields();
}
function clearInoutsFields()
{
let inputs = document.querySelectorAll("input");
inputs.forEach((input) => (input.value = ""));
}
function sendLocal()
{
localStorage.setItem('detail', JSON.stringify(transactions));
localStorage.setItem('amountTotal', amountTotal.innerHTML);
localStorage.setItem('incomeTotal', incomeTotal.innerHTML);
localStorage.setItem('expenseTotal', expenseTotal.innerHTML);
}
function retrieveLocal()
{
dispArr = JSON.parse(localStorage.getItem('detail'));
//return 1;
}