-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinvoice.js
88 lines (71 loc) · 1.62 KB
/
invoice.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
function createInvoice(services = {}) {
return {
phone: services.phone || 3300,
internet: services.internet || 5000,
payments: [],
total() {
return this.phone + this.internet;
},
addPayment(payment) {
this.payments.push(payment);
},
addPayments(payment) {
this.payments = this.payments.concat(payment);
},
amountDue(){
function paymentTotal(payments) {
let total = 0;
let i;
for (i = 0; i < payments.length; i += 1) {
total += payments[i].total();
}
return total;
}
return this.total() - paymentTotal(this.payments);
},
};
}
function invoiceTotal(invoices) {
let total = 0;
let i;
for (i = 0; i < invoices.length; i += 1) {
total += invoices[i].total();
}
return total;
}
function createPayment(services = {}) {
let payment = {
phone: services.phone || 0,
internet: services.internet || 0,
amount: services.amount,
};
payment.total = function() {
return this.amount || (this.phone + this.internet);
};
return payment;
}
function paymentTotal(payments) {
let total = 0;
let i;
for (i = 0; i < payments.length; i += 1) {
total += payments[i].total();
}
return total;
}
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