-
Notifications
You must be signed in to change notification settings - Fork 5
/
08-advanced-scope-exercise.js
54 lines (41 loc) · 1.19 KB
/
08-advanced-scope-exercise.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
function getStudentFromId(studentId) {
return studentRecords.find(matchId);
// Must be nested because of the reference to studentId
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;
}