-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
113 lines (100 loc) · 2.87 KB
/
app.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
let transactionss = [{
id: 1,
sourceAccount: 'my_account',
targetAccount: 'coffee_shop',
amount: -30,
category: 'eating_out1',
time: '2018-03-01T12:34:00Z'
},{
id: 2,
sourceAccount: 'my_account',
targetAccount: 'coffee_shop',
amount: -30,
category: 'eating_out2',
time: '2018-03-02T12:34:00Z'
},{
id: 3,
sourceAccount: 'my_account',
targetAccount: 'coffee_shop',
amount: 30,
category: 'eating_out3',
time: '2018-03-03T12:34:00Z'
},{
id: 4,
sourceAccount: 'my_account',
targetAccount: 'coffee_shop',
amount: -30,
category: 'eating_out4',
time: '2018-03-04T12:34:00Z'
},{
id: 5,
sourceAccount: 'my_account',
targetAccount: 'coffee_shop',
amount: -30,
category: 'eating_out5',
time: '2018-03-31T12:34:00Z'
}];
startDate = new Date("2018-03-01T12:34:00Z");
endDate = new Date("2018-03-31T12:34:00Z");
xcategory = 'eating_out4';
function getAmountByCategoryInPeriod (transactions = [], category, start, end) {
let transactionsAftherFilter = transactions.filter(x=>filterProcess(x,category, start, end));
result = transactionsAftherFilter.reduce(function (accumulator, currentValue) {
return accumulator + currentValue.amount;
},0);
console.log(result);
}
getAmountByCategoryInPeriod(transactionss,)
function filterProcess(x,category,startDate,endDate){
//vDates: Validate dates
let vDates = (timeTransaction,dateFrom,dateTo)=>{
let timeTrasactionFormat = new Date(timeTransaction);
return timeTrasactionFormat >= dateFrom && timeTrasactionFormat <= dateTo;
}
//vCategory: Validate Category
let vCategory = (xCategory,category)=> xCategory == category;
let xPrice = (price)=> price < 0;
let vResult = vDates(x.time,startDate,endDate)
&& vCategory(x.category,category)
&&xPrice(x.amount);
return vResult;
}
let transactions = [{
id: 1,
sourceAccount: 'my_account',
targetAccount: 'coffee_shop',
amount: -30,
category: 'eating_out',
time: '2018-03-01T12:34:00Z'
},{
id: 2,
sourceAccount: 'my_account',
targetAccount: 'coffee_shop',
amount: -30,
category: 'eating_out',
time: '2018-03-02T12:34:00Z'
},,{
id: 3,
sourceAccount: 'my_account',
targetAccount: 'coffee_shop',
amount: -30,
category: 'eating_out',
time: '2018-03-02T12:34:00Z'
},{
id: 4,
sourceAccount: 'my_account',
targetAccount: 'coffee_shop',
amount: -30,
category: 'eating_out',
time: '2018-03-02T12:34:00Z'
}];
let startDate = new Date("2018-03-01");
let endDate = new Date("2018-03-31");
let transactionsFilterDate = transactions.find(x=>applicateDataFormat(x.time,startDate,endDate));
console.log(transactionsFilterDate);
//Working whit the date using
function applicateDataFormat(timeTransaction,dateFrom,dateTo){
let timeTrasactionFormat = new Date(timeTransaction);
return (timeTrasactionFormat >= dateFrom && timeTrasactionFormat <= dateTo);
}
tr;onsFilterDate