-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcimpress
109 lines (79 loc) · 3.03 KB
/
cimpress
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
function highOrderFun() {
return function() {
//
}
}
// url="xyz.com"
// function fun() {
// const response = fetch
// }
// Given a cart with products and Coupons
// N% off coupons, such as a “10% off coupon”
// P% off the next item coupons , such as “take 20% off the next item in your cart”
// $D off of the Nth product of type T, such as “take $5 off your third business card holder”
// $10 postcard sorter
// Coupon: Take $2 off your 2nd postcard sorter
// Coupon: 25% off each individual item
// Coupon: Take 10% off the next item in the cart
//
// $10 postcard sorter
// Total = ($10 * 75%) + (($10 - $2) * 75% * 90%) = $7.50 + $5.40 = $12.90
interface CouponItem {
type: "PRODUCT" | "COUPON"
percentage: Number
appliedOn: "ALL_ITEM" | "NEXT_ITEM" | "N%_ON_NTH_ITEM"
nth: Number
productType: String
}
interface Item {
type: "PRODUCT" | "COUPON"
price: Number
name: String
productType: String
price: Number
details: ...
}
class Cart {
const items: Items | CouptonItem [];
const nthItemCoupons = {};
const
function Cart() {
this.items = items;
}
// function
function getCartTotal() {
let cartTotal = 0;
const allItemsCoupon = this.items.filter(item => item.type === 'COUPON' && item.appliedOn === 'ALL_ITEMS')
const totalOffAllItems = allItemsCoupon.reduce((total, item) => total + item.percentage, 0);
const nthItemCoupon = this.items.forEach(item =>
{ if (item.type === 'COUPON' && item.appliedOn === 'N%_ON_NTH_ITEM') {
this.nthItemCoupons[item.productType] = {
order: item.nth;
percentage: item.percentage
}
}
});
const productOrder = {};
this.items.forEarch((item, index) => {
const itemType = item.type;
if (itemType === 'PRODUCT') {
if (productOrder[item.productType] !=== undefined) {
productOrder[item.productType] += 1;
}
// apply all_items coupon
let effectivePrice = item.price - (item.price * totalOffAllItems)/100;
// apply next item coupon if any
if (index > 0 && this.items[index-1].type === 'COUPON' && this.items[index-1].appliedOn === 'NEXT_ITEM') {
effectivePrice -= (item.price*this.items[index-1].percentage)/100;
}
// apply third type of coupon
if (this.nthItemCoupons[item.type] !== undefined) {
this.nthItemCoupons[item.type]['order'] === productOrder[item.type]
} {
effectivePrice -= (item.price*this.nthItemCoupons[item.type].percentage)/100;
}
cartTotal += effectivePrice;
}
})
}
}