-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinvoices.js
More file actions
41 lines (35 loc) · 912 Bytes
/
invoices.js
File metadata and controls
41 lines (35 loc) · 912 Bytes
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
let invoices = {
unpaid: [],
add: function(name, amount) {
this.unpaid.push({name, amount});
},
};
invoices.totalDue = function() {
return this.unpaid.reduce((sum, invoice) => {
return sum + invoice.amount;
}, 0);
};
invoices.add('Due North Development', 250);
invoices.add('Moonbeam Interactive', 187.50);
invoices.add('Slough Digital', 300);
invoices.paid = [];
invoices.payInvoice = function(name) {
let newUnpaid = [];
this.unpaid.forEach(invoice => {
if (invoice.name === name) {
this.paid.push(invoice);
} else {
newUnpaid.push(invoice);
}
});
this.unpaid = newUnpaid;
};
invoices.totalPaid = function() {
return this.paid.reduce((sum, invoice) => {
return sum + invoice.amount;
}, 0);
};
invoices.payInvoice('Due North Development');
invoices.payInvoice('Slough Digital');
console.log(invoices.totalPaid());
console.log(invoices.totalDue());