-
Notifications
You must be signed in to change notification settings - Fork 0
/
04_deep_foundations_function_scope_arrow.js
73 lines (63 loc) · 1.89 KB
/
04_deep_foundations_function_scope_arrow.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
function printRecords(recordIds) {
return studentRecords.filter(student => {
return recordIds.find(x => { return student.id == x; });
}).sort((firstStudent, secondStudent) => {
return firstStudent.name > secondStudent.name ? 1 : -1;
}).forEach(student => {
console.log(`${student.name} (${student.id}) ${isPaidToString(student.paid)}`);
});
function isPaidToString(paid) {
return paid ? 'Paid' : 'Not paid';
}
}
function filterBy(list, filterCondition) {
return studentRecords.filter(student => {
return list.find(id => { return filterCondition(id, student) });
}).map(student => { return student.id; });
}
function paidStudentsToEnroll() {
return filterBy(currentEnrollment, (id, student) => {
return id != student.id && student.paid;
}).concat(currentEnrollment);
}
function remindUnpaid(recordIds) {
return filterBy(recordIds, (id, student) => {
return id == student.id && student.paid == false;
});
}
// ********************************
var currentEnrollment = [ 410, 105, 664, 375 ];
var studentRecords = [
{ id: 313, name: "Frank", paid: true, },
{ id: 410, name: "Suzy", paid: true, },
{ id: 709, name: "Brian", paid: false, },
{ id: 105, name: "Henry", paid: false, },
{ id: 502, name: "Mary", paid: true, },
{ id: 664, name: "Bob", paid: false, },
{ id: 250, name: "Peter", paid: true, },
{ id: 375, name: "Sarah", paid: true, },
{ id: 867, name: "Greg", paid: false, },
];
printRecords(currentEnrollment);
console.log("----");
currentEnrollment = paidStudentsToEnroll();
printRecords(currentEnrollment);
console.log("----");
printRecords(remindUnpaid(currentEnrollment));
/*
Bob (664): Not Paid
Henry (105): Not Paid
Sarah (375): Paid
Suzy (410): Paid
----
Bob (664): Not Paid
Frank (313): Paid
Henry (105): Not Paid
Mary (502): Paid
Peter (250): Paid
Sarah (375): Paid
Suzy (410): Paid
----
Bob (664): Not Paid
Henry (105): Not Paid
*/