Skip to content

Commit 02df3c4

Browse files
authored
deepJS exercises
1 parent 45795a8 commit 02df3c4

File tree

25 files changed

+1579
-0
lines changed

25 files changed

+1579
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# `class`
2+
3+
In this exercise, you will refactor some code that manages student enrollment records for a workshop, from the namespace pattern to the `class` pattern.
4+
5+
## Instructions
6+
7+
1. Define a class called `Helpers` that includes the functions that are not `this`-aware.
8+
9+
2. Define a class called `Workshop` that extends `Helpers`, which includes all the other functions. Hint: `constructor()` and `super()`.
10+
11+
3. Instantiate the `Workshop` class as `deepJS`.
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
class Helpers {
2+
sortByNameAsc(record1,record2) {
3+
if (record1.name < record2.name) return -1;
4+
else if (record1.name > record2.name) return 1;
5+
else return 0;
6+
}
7+
8+
printRecord(record) {
9+
console.log(`${record.name} (${record.id}): ${record.paid ? "Paid" : "Not Paid"}`);
10+
}
11+
}
12+
13+
class Workshop extends Helpers {
14+
constructor() {
15+
super();
16+
this.currentEnrollment = [];
17+
this.studentRecords = [];
18+
}
19+
20+
addStudent(id,name,paid) {
21+
this.studentRecords.push({ id, name, paid, });
22+
}
23+
24+
enrollStudent(id) {
25+
if (!this.currentEnrollment.includes(id)) {
26+
this.currentEnrollment.push(id);
27+
}
28+
}
29+
30+
printCurrentEnrollment() {
31+
this.printRecords(this.currentEnrollment);
32+
}
33+
34+
enrollPaidStudents() {
35+
this.currentEnrollment = this.paidStudentsToEnroll();
36+
this.printCurrentEnrollment();
37+
}
38+
39+
remindUnpaidStudents() {
40+
this.remindUnpaid(this.currentEnrollment);
41+
}
42+
43+
getStudentFromId(studentId) {
44+
return this.studentRecords.find(matchId);
45+
46+
// *************************
47+
48+
function matchId(record) {
49+
return (record.id == studentId);
50+
}
51+
}
52+
53+
printRecords(recordIds) {
54+
var records = recordIds.map(this.getStudentFromId.bind(this));
55+
56+
records.sort(this.sortByNameAsc);
57+
58+
records.forEach(this.printRecord);
59+
}
60+
61+
paidStudentsToEnroll() {
62+
var recordsToEnroll = this.studentRecords.filter(this.needToEnroll.bind(this));
63+
64+
var idsToEnroll = recordsToEnroll.map(this.getStudentId);
65+
66+
return [ ...this.currentEnrollment, ...idsToEnroll ];
67+
}
68+
69+
needToEnroll(record) {
70+
return (record.paid && !this.currentEnrollment.includes(record.id));
71+
}
72+
73+
getStudentId(record) {
74+
return record.id;
75+
}
76+
77+
remindUnpaid(recordIds) {
78+
var unpaidIds = recordIds.filter(this.notYetPaid.bind(this));
79+
80+
this.printRecords(unpaidIds);
81+
}
82+
83+
notYetPaid(studentId) {
84+
var record = this.getStudentFromId(studentId);
85+
return !record.paid;
86+
}
87+
}
88+
89+
90+
// ********************************
91+
92+
var deepJS = new Workshop();
93+
94+
deepJS.addStudent(311,"Frank",/*paid=*/true);
95+
deepJS.addStudent(410,"Suzy",/*paid=*/true);
96+
deepJS.addStudent(709,"Brian",/*paid=*/false);
97+
deepJS.addStudent(105,"Henry",/*paid=*/false);
98+
deepJS.addStudent(502,"Mary",/*paid=*/true);
99+
deepJS.addStudent(664,"Bob",/*paid=*/false);
100+
deepJS.addStudent(250,"Peter",/*paid=*/true);
101+
deepJS.addStudent(375,"Sarah",/*paid=*/true);
102+
deepJS.addStudent(867,"Greg",/*paid=*/false);
103+
104+
deepJS.enrollStudent(410);
105+
deepJS.enrollStudent(105);
106+
deepJS.enrollStudent(664);
107+
deepJS.enrollStudent(375);
108+
109+
deepJS.printCurrentEnrollment();
110+
console.log("----");
111+
deepJS.enrollPaidStudents();
112+
console.log("----");
113+
deepJS.remindUnpaidStudents();
114+
115+
/*
116+
Bob (664): Not Paid
117+
Henry (105): Not Paid
118+
Sarah (375): Paid
119+
Suzy (410): Paid
120+
----
121+
Bob (664): Not Paid
122+
Frank (313): Paid
123+
Henry (105): Not Paid
124+
Mary (502): Paid
125+
Peter (250): Paid
126+
Sarah (375): Paid
127+
Suzy (410): Paid
128+
----
129+
Bob (664): Not Paid
130+
Henry (105): Not Paid
131+
*/
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
var deepJS = {
2+
currentEnrollment: [],
3+
studentRecords: [],
4+
addStudent(id,name,paid) {
5+
this.studentRecords.push({ id, name, paid, });
6+
},
7+
enrollStudent(id) {
8+
if (!this.currentEnrollment.includes(id)) {
9+
this.currentEnrollment.push(id);
10+
}
11+
},
12+
printCurrentEnrollment() {
13+
this.printRecords(this.currentEnrollment);
14+
},
15+
enrollPaidStudents() {
16+
this.currentEnrollment = this.paidStudentsToEnroll();
17+
this.printCurrentEnrollment();
18+
},
19+
remindUnpaidStudents() {
20+
this.remindUnpaid(this.currentEnrollment);
21+
},
22+
getStudentFromId(studentId) {
23+
return this.studentRecords.find(matchId);
24+
25+
// *************************
26+
27+
function matchId(record) {
28+
return (record.id == studentId);
29+
}
30+
},
31+
printRecords(recordIds) {
32+
var records = recordIds.map(this.getStudentFromId.bind(this));
33+
34+
records.sort(this.sortByNameAsc);
35+
36+
records.forEach(this.printRecord);
37+
},
38+
sortByNameAsc(record1,record2){
39+
if (record1.name < record2.name) return -1;
40+
else if (record1.name > record2.name) return 1;
41+
else return 0;
42+
},
43+
printRecord(record) {
44+
console.log(`${record.name} (${record.id}): ${record.paid ? "Paid" : "Not Paid"}`);
45+
},
46+
paidStudentsToEnroll() {
47+
var recordsToEnroll = this.studentRecords.filter(this.needToEnroll.bind(this));
48+
49+
var idsToEnroll = recordsToEnroll.map(this.getStudentId);
50+
51+
return [ ...this.currentEnrollment, ...idsToEnroll ];
52+
},
53+
needToEnroll(record) {
54+
return (record.paid && !this.currentEnrollment.includes(record.id));
55+
},
56+
getStudentId(record) {
57+
return record.id;
58+
},
59+
remindUnpaid(recordIds) {
60+
var unpaidIds = recordIds.filter(this.notYetPaid.bind(this));
61+
62+
this.printRecords(unpaidIds);
63+
},
64+
notYetPaid(studentId) {
65+
var record = this.getStudentFromId(studentId);
66+
return !record.paid;
67+
}
68+
};
69+
70+
71+
// ********************************
72+
73+
deepJS.addStudent(311,"Frank",/*paid=*/true);
74+
deepJS.addStudent(410,"Suzy",/*paid=*/true);
75+
deepJS.addStudent(709,"Brian",/*paid=*/false);
76+
deepJS.addStudent(105,"Henry",/*paid=*/false);
77+
deepJS.addStudent(502,"Mary",/*paid=*/true);
78+
deepJS.addStudent(664,"Bob",/*paid=*/false);
79+
deepJS.addStudent(250,"Peter",/*paid=*/true);
80+
deepJS.addStudent(375,"Sarah",/*paid=*/true);
81+
deepJS.addStudent(867,"Greg",/*paid=*/false);
82+
83+
deepJS.enrollStudent(410);
84+
deepJS.enrollStudent(105);
85+
deepJS.enrollStudent(664);
86+
deepJS.enrollStudent(375);
87+
88+
deepJS.printCurrentEnrollment();
89+
console.log("----");
90+
deepJS.enrollPaidStudents();
91+
console.log("----");
92+
deepJS.remindUnpaidStudents();
93+
94+
/*
95+
Bob (664): Not Paid
96+
Henry (105): Not Paid
97+
Sarah (375): Paid
98+
Suzy (410): Paid
99+
----
100+
Bob (664): Not Paid
101+
Frank (313): Paid
102+
Henry (105): Not Paid
103+
Mary (502): Paid
104+
Peter (250): Paid
105+
Sarah (375): Paid
106+
Suzy (410): Paid
107+
----
108+
Bob (664): Not Paid
109+
Henry (105): Not Paid
110+
*/
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# `this`
2+
3+
In this exercise, you will refactor some code that manages student enrollment records for a workshop, from the module pattern to the namespace pattern using the `this` keyword.
4+
5+
## Instructions
6+
7+
1. Remove the `defineWorkshop()` module factory, and replace it with an object literal (named `deepJS`) that holds all the module's functions, as well as the `currentEnrollment` and `studentRecords` data arrays.
8+
9+
2. Change all internal function references and references to the data arrays to use the `this` keyword prefix.
10+
11+
3. Make sure any place where a `this`-aware callback is passed is hard-bound with `bind(..)`. Don't `bind(..)` a function reference if it's not `this`-aware.
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
var deepJS = {
2+
currentEnrollment: [],
3+
studentRecords: [],
4+
addStudent(id,name,paid) {
5+
this.studentRecords.push({ id, name, paid, });
6+
},
7+
enrollStudent(id) {
8+
if (!this.currentEnrollment.includes(id)) {
9+
this.currentEnrollment.push(id);
10+
}
11+
},
12+
printCurrentEnrollment() {
13+
this.printRecords(this.currentEnrollment);
14+
},
15+
enrollPaidStudents() {
16+
this.currentEnrollment = this.paidStudentsToEnroll();
17+
this.printCurrentEnrollment();
18+
},
19+
remindUnpaidStudents() {
20+
this.remindUnpaid(this.currentEnrollment);
21+
},
22+
getStudentFromId(studentId) {
23+
return this.studentRecords.find(matchId);
24+
25+
// *************************
26+
27+
function matchId(record) {
28+
return (record.id == studentId);
29+
}
30+
},
31+
printRecords(recordIds) {
32+
var records = recordIds.map(this.getStudentFromId.bind(this));
33+
34+
records.sort(this.sortByNameAsc);
35+
36+
records.forEach(this.printRecord);
37+
},
38+
sortByNameAsc(record1,record2){
39+
if (record1.name < record2.name) return -1;
40+
else if (record1.name > record2.name) return 1;
41+
else return 0;
42+
},
43+
printRecord(record) {
44+
console.log(`${record.name} (${record.id}): ${record.paid ? "Paid" : "Not Paid"}`);
45+
},
46+
paidStudentsToEnroll() {
47+
var recordsToEnroll = this.studentRecords.filter(this.needToEnroll.bind(this));
48+
49+
var idsToEnroll = recordsToEnroll.map(this.getStudentId);
50+
51+
return [ ...this.currentEnrollment, ...idsToEnroll ];
52+
},
53+
needToEnroll(record) {
54+
return (record.paid && !this.currentEnrollment.includes(record.id));
55+
},
56+
getStudentId(record) {
57+
return record.id;
58+
},
59+
remindUnpaid(recordIds) {
60+
var unpaidIds = recordIds.filter(this.notYetPaid.bind(this));
61+
62+
this.printRecords(unpaidIds);
63+
},
64+
notYetPaid(studentId) {
65+
var record = this.getStudentFromId(studentId);
66+
return !record.paid;
67+
}
68+
};
69+
70+
71+
// ********************************
72+
73+
deepJS.addStudent(311,"Frank",/*paid=*/true);
74+
deepJS.addStudent(410,"Suzy",/*paid=*/true);
75+
deepJS.addStudent(709,"Brian",/*paid=*/false);
76+
deepJS.addStudent(105,"Henry",/*paid=*/false);
77+
deepJS.addStudent(502,"Mary",/*paid=*/true);
78+
deepJS.addStudent(664,"Bob",/*paid=*/false);
79+
deepJS.addStudent(250,"Peter",/*paid=*/true);
80+
deepJS.addStudent(375,"Sarah",/*paid=*/true);
81+
deepJS.addStudent(867,"Greg",/*paid=*/false);
82+
83+
deepJS.enrollStudent(410);
84+
deepJS.enrollStudent(105);
85+
deepJS.enrollStudent(664);
86+
deepJS.enrollStudent(375);
87+
88+
deepJS.printCurrentEnrollment();
89+
console.log("----");
90+
deepJS.enrollPaidStudents();
91+
console.log("----");
92+
deepJS.remindUnpaidStudents();
93+
94+
/*
95+
Bob (664): Not Paid
96+
Henry (105): Not Paid
97+
Sarah (375): Paid
98+
Suzy (410): Paid
99+
----
100+
Bob (664): Not Paid
101+
Frank (313): Paid
102+
Henry (105): Not Paid
103+
Mary (502): Paid
104+
Peter (250): Paid
105+
Sarah (375): Paid
106+
Suzy (410): Paid
107+
----
108+
Bob (664): Not Paid
109+
Henry (105): Not Paid
110+
*/

0 commit comments

Comments
 (0)