-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReward
More file actions
65 lines (52 loc) · 1.75 KB
/
Reward
File metadata and controls
65 lines (52 loc) · 1.75 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
// offers a rewards program to customers, awarding points based on each recorded purchase.
function calRewards(price) {
if (price >=50 && price < 100) {
return price-50;
} else if (price >100){
return (2*(price-100) + 50);
}
return 0;
}
class Transaction {
constructor(price) {
this.price = price;
this.rewards = calRewards(price);
this.tranDate = new Date();
}
}
class TransactionList {
constructor() {
this.list = [];
}
getLast3MonthsList() {
var today = new Date();
const threeOldDate = today.setMonth(today.getMonth() - 3);
let filteredList = this.list.filter(trans => trans.tranDate > threeOldDate);
return filteredList.sort((a,b) => b.transactionDate - a.tranDate);
}
getAllTransactions() {
return this.list.sort((a,b) => b.tranDate-a.tranDate);
}
addTransaction(price) {
const transaction = new Transaction(price);
this.list.push(transaction);
}
getTotalRewards() {
return this.list.length ? this.list.reduce((acc,key)=>key.rewards+acc, 0) : 0;
}
rewardPerMonth() {
let last3MonthRewardsInDesc = [];
for(let i=0; i<3; i++) {
let filteredList = this.list.filter(trans => trans.transactionDate.getMonth() == (new Date).getMonth() - i );
last3MonthRewardsInDesc[i] = filteredList.reduce((acc,key)=>key.rewards+acc,0);
}
return last3MonthRewardsInDesc;
}
}
let TransactionList = new TransactionList();
TransactionList.addTransaction(14);
TransactionList.addTransaction(540);
TransactionList.addTransaction(2000);
TransactionList.addTransaction(250);
TransactionList.addTransaction(3600);
let arr = TransactionList.getAllTransactions();