-
Notifications
You must be signed in to change notification settings - Fork 0
/
05_deep_foundations_hoisting_resolved.js
96 lines (75 loc) · 2.08 KB
/
05_deep_foundations_hoisting_resolved.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
89
90
91
92
93
94
95
96
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("----");
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
*/
// ********************************
function getStudentFromId(studentId) {
return studentRecords.find(matchId);
// *************************
function matchId(record) {
return (record.id == studentId);
}
}
function printRecords(recordIds) {
var records = recordIds.map(getStudentFromId);
records.sort(sortByNameAsc);
records.forEach(printRecord);
}
function sortByNameAsc(record1,record2){
if (record1.name < record2.name) return -1;
else if (record1.name > record2.name) return 1;
else return 0;
}
function printRecord(record) {
console.log(`${record.name} (${record.id}): ${record.paid ? "Paid" : "Not Paid"}`);
}
function paidStudentsToEnroll() {
var recordsToEnroll = studentRecords.filter(needToEnroll);
var idsToEnroll = recordsToEnroll.map(getStudentId);
return [ ...currentEnrollment, ...idsToEnroll ];
}
function needToEnroll(record) {
return (record.paid && !currentEnrollment.includes(record.id));
}
function getStudentId(record) {
return record.id;
}
function remindUnpaid(recordIds) {
var unpaidIds = recordIds.filter(notYetPaid);
printRecords(unpaidIds);
}
function notYetPaid(studentId) {
var record = getStudentFromId(studentId);
return !record.paid;
}