-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactoryFunctions.js
More file actions
212 lines (175 loc) · 5.56 KB
/
factoryFunctions.js
File metadata and controls
212 lines (175 loc) · 5.56 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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// 2. Rewrite the code below to use object-literal syntax to generate the returned object:
// function makeObj() {
// let obj = {};
// obj.propA = 10;
// obj.propB = 20;
// return obj;
// }
// function makeObj() {
// return {
// propA: 10,
// propB: 20,
// };
// }
// 3. In the following problems, we'll be working through an invoice processing program.
// To get you started, we provide you with the following code that can process one invoice:
// let invoice = {
// phone: 3000,
// internet: 6500,
// };
// let payment = {
// phone: 1300,
// internet: 5500,
// };
// let invoiceTotal = invoice.phone + invoice.internet;
// let paymentTotal = payment.phone + payment.internet;
// let remainingDue = invoiceTotal - paymentTotal;
// console.log(paymentTotal); // => 6800
// console.log(remainingDue); // => 2700
// To be able to process multiple invoices, we'll need to have a factory function that we can use to create invoices.
// The requirements for this factory function are the following:
// - It returns an invoice object, with phone and internet properties, and a total method.
// - The default value for the phone service is 3000, and the internet service is 5500 (in cents, of course!).
// - The function takes an object argument with attributes to override the default values.
// Your implemented function should be able to work with the code below:
function createInvoice(services = {}) {
let phone = services['phone'] || 3000;
let internet = services['internet'] || 5500;
return {
phone,
internet,
total() {
return this.phone + this.internet;
},
amountDue() { return this.total(); },
addPayment(payment) {
if (payment.amount) {
if (this.phone > payment.amount) {
this.phone -= payment.amount;
payment.amount = 0;
} else if (this.internet > payment.amount) {
this.internet -= payment.amount;
payment.amount = 0;
} else {
let phonePayment = payment.amount - this.phone;
this.phone -= phonePayment;
payment.amount -= phonePayment;
this.internet -= payment.amount;
}
} else {
this.phone -= payment.phone || 0;
this.internet -= payment.internet || 0;
}
},
addPayments(payments) {
if (!Array.isArray(payments)) {
payments = [payments];
}
payments.forEach(payment => {
if (payment.amount) {
if (this.phone > payment.amount) {
this.phone -= payment.amount;
payment.amount = 0;
} else if (this.internet > payment.amount) {
this.internet -= payment.amount;
payment.amount = 0;
} else {
let phonePayment = payment.amount - this.phone;
this.phone -= phonePayment;
payment.amount -= phonePayment;
this.internet -= payment.amount;
}
} else {
this.phone -= payment.phone || 0;
this.internet -= payment.internet || 0;
}
}, this);
},
};
}
function invoiceTotal(invoices) {
let total = 0;
let i;
for (i = 0; i < invoices.length; i += 1) {
total += invoices[i].total();
}
return total;
}
let invoices = [];
invoices.push(createInvoice());
invoices.push(createInvoice({
internet: 6500,
}));
invoices.push(createInvoice({
phone: 2000,
}));
invoices.push(createInvoice({
phone: 1000,
internet: 4500,
}));
console.log(invoiceTotal(invoices)); // => 31000
// 4. Now let's build a factory function to create payments. The function can take an object argument in one of 3 forms:
// - Payment for one service, such as: {internet: 1000} or {phone: 1000}
// - Payment for both services, such as: {internet: 2000, phone: 1000}.
// - Payment with just an amount property, such as: {amount: 2000}.
// and should return an object that has paid services, and a total method that returns the payment total.
// If the amount property is not present, this should return the sum of phone and internet services;
// if the amount property is present, just return the value of that property.
// Your code should work with the following:
function createPayment(services = {}) {
// implement the factory function here
return {
internet: services.internet || 0,
phone: services.phone || 0,
amount: services.amount || null,
total() {
if (this.amount) {
return this.amount;
} else {
return this.internet + this.phone;
}
},
};
}
function paymentTotal(payments) {
let total = 0;
let i;
for (i = 0; i < payments.length; i += 1) {
total += payments[i].total();
}
return total;
}
let payments = [];
payments.push(createPayment());
payments.push(createPayment({
internet: 6500,
}));
payments.push(createPayment({
phone: 2000,
}));
payments.push(createPayment({
phone: 1000,
internet: 4500,
}));
payments.push(createPayment({
amount: 10000,
}));
console.log(paymentTotal(payments)); // => 24000
// 5. Update your createInvoice function to make it possible to add payment(s) to invoices. Use the code below as a guideline:
let invoice = createInvoice({
phone: 1200,
internet: 4000,
});
let payment1 = createPayment({
amount: 2000,
});
let payment2 = createPayment({
phone: 1000,
internet: 1200,
});
let payment3 = createPayment({
phone: 1000,
});
invoice.addPayment(payment1);
invoice.addPayments([payment2, payment3]);
console.log(invoice.amountDue()); // this should return 0