-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExerciseAmazon.js
77 lines (62 loc) · 1.69 KB
/
ExerciseAmazon.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
//Functional programming Exercise.
const user = {
name: "kim",
active: true,
cart: [],
purchases: [],
};
const userHistory = [];
function addToCart(user, item) {
userHistory.push(
Object.assign({}, user, { cart: user.cart, purchases: user.purchases })
); // Noting every transaction, It will help to trace back in case of complain
const updateCart = user.cart.concat(item);
return Object.assign({}, user, { cart: updateCart });
}
function taxItem(user) {
userHistory.push(
Object.assign({}, user, { cart: user.cart, purchases: user.purchases })
);
const { cart } = user;
const taxRate = 1.4;
const updatedCart = cart.map((item) => {
return {
name: item.name,
price: item.price * taxRate,
};
});
return Object.assign({}, user, { cart: updatedCart });
}
function buyItem(user) {
userHistory.push(
Object.assign({}, user, { cart: user.cart, purchases: user.purchases })
);
return Object.assign({}, user, { purchases: user.cart });
}
function emptyCart(user) {
userHistory.push(
Object.assign({}, user, { cart: user.cart, purchases: user.purchases })
);
return Object.assign({}, user, { cart: [] });
}
//To Refund
// function refundItem(user, item) {
// const { purchases } = user;
// const refundItem = purchases.splice(item);
// return Object.assign({}, user, { purchases: refundItem });
// }
//Creating compose
const compose =
(fn1, fn2) =>
(...args) =>
fn1(fn2(...args));
const purchaseItems = (...fns) => fns.reduce(compose);
purchaseItems(
emptyCart,
buyItem,
taxItem,
addToCart
)(user, { name: "laptop", price: 200 });
//To Refund
// refundItem(user, { name: "laptop", price: 200 });
console.log(userHistory);