-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
71 lines (62 loc) · 1.48 KB
/
Copy pathscript.js
File metadata and controls
71 lines (62 loc) · 1.48 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
function calculateTotal(type) {
let sum = 0;
data.allItems[type].forEach(cur => {
sum += cur.value;
});
data.totals[type] = sum;
}
function calculateBudget() {
calculateTotal('exp');
calculateTotal('inc');
data.budget = data.totals.inc - data.totals.exp;
if (data.totals.inc > 0) {
// Using Math.floor to always round down
data.percentage = Math.floor((data.totals.exp / data.totals.inc) * 100);
} else {
data.percentage = -1;
}
}
var data = {
allItems: {
exp: [],
inc: []
},
totals: {
exp: 0,
inc: 0
},
budget: 0,
percentage: -1
};
var getBudget = function() {
return {
budget: data.budget,
totalInc: data.totals.inc,
totalExp: data.totals.exp,
percentage: data.percentage
};
};
function addItem(type, description, value) {
const ID = data.allItems[type].length > 0 ? data.allItems[type][data.allItems[type].length - 1].id + 1 : 1;
data.allItems[type].push({ id: ID, description, value });
calculateTotal(type);
}
function deleteItem(type, id) {
var ids = data.allItems[type].map(function(current) {
return current.id;
});
var index = ids.indexOf(id);
if (index !== -1) {
data.allItems[type].splice(index, 1);
calculateTotal(type);
calculateBudget();
}
}
module.exports = {
calculateBudget,
calculateTotal,
getBudget,
addItem,
deleteItem,
data
};