Welcome to today's lesson! We'll be exploring a practical application of JavaScript's Set
object by managing student enrollments for various courses. Imagine you're running an online course platform and need to handle enrollments, checks, and listings of students in different courses. Sets
are perfect for this kind of problem since they don’t allow duplicates, ensuring that a student can't enroll in the same course more than once!
By the end of this session, you'll be well-versed in using Sets
for such tasks. Let’s dive in!
Here are the methods we need to implement in our enrollment system:
enroll(student, course)
: This method adds a student to a course. If the student is already enrolled, it does nothing.unenroll(student, course)
: This method removes a student from a course. It returnstrue
if the student was enrolled and has now been removed. Otherwise, it returnsfalse
. If, after unenrolling the student, the course becomes empty (no one is enrolled there), remove the course as well.isEnrolled(student, course)
: This method checks if a student is enrolled in a course. It returnstrue
if the student is enrolled andfalse
otherwise.listStudents(course)
: This method returns an array of all students enrolled in a given course. If no students are enrolled, it returns an empty array.
Let's look at how to implement each of these methods step-by-step.
We'll start by defining our class and then add each method one by one.
First, we define our EnrollmentSystem
class:
JavaScript1class EnrollmentSystem { 2 constructor() { 3 this.enrollments = {}; 4 } 5}
This code initializes an EnrollmentSystem
class with an object named enrollments
that maps courses to sets of students.
Next, we implement the enroll
method:
JavaScript1class EnrollmentSystem { 2 constructor() { 3 this.enrollments = {}; 4 } 5 6 enroll(student, course) { 7 if (!this.enrollments[course]) { 8 this.enrollments[course] = new Set(); 9 } 10 this.enrollments[course].add(student); 11 } 12} 13 14// Example usage: 15const es = new EnrollmentSystem(); 16es.enroll("Alice", "Math101"); 17console.log(es.enrollments); // Output: { Math101: Set(1) { 'Alice' } }
In the enroll
method, the console.log
statement outputs the elements in a specific order based on the operations performed prior to it. Here's a breakdown:
this.enrollments
is an object that maps course names to sets of students.- When
es.enroll("Alice", "Math101")
is called:- It checks if "Math101" exists in
this.enrollments
. Since it doesn't, a newSet
is created for "Math101". - Alice is then added to the set for "Math101".
- It checks if "Math101" exists in
Thus, console.log(es.enrollments)
outputs the following:
JavaScript1{ 2 Math101: Set(1) { 'Alice' } 3}
The Set
object for "Math101" contains one element: 'Alice'. Sets in JavaScript maintain insertion order, so if more students are added to the same course, they will appear in the order in which they were added.
Let's move on to the unenroll
method:
JavaScript1class EnrollmentSystem { 2 constructor() { 3 this.enrollments = {}; 4 } 5 6 enroll(student, course) { 7 if (!this.enrollments[course]) { 8 this.enrollments[course] = new Set(); 9 } 10 this.enrollments[course].add(student); 11 } 12 13 unenroll(student, course) { 14 if (this.enrollments[course] && this.enrollments[course].has(student)) { 15 this.enrollments[course].delete(student); 16 if (this.enrollments[course].size === 0) { // If the set is empty, remove the course 17 delete this.enrollments[course]; 18 } 19 return true; 20 } 21 return false; 22 } 23} 24 25// Example usage: 26const es = new EnrollmentSystem(); 27es.enroll("Alice", "Math101"); 28es.enroll("Bob", "Math101"); 29console.log(es.enrollments); // Output: { Math101: Set(2) { 'Alice', 'Bob' } } 30es.unenroll("Alice", "Math101"); 31console.log(es.enrollments); // Output: { Math101: Set(1) { 'Bob' } } 32es.unenroll("Bob", "Math101"); 33console.log(es.enrollments); // Output: {}
This method first checks whether the course
exists in the enrollments and whether the student
is enrolled in that course. If they are, it removes the student from the course. If the course set becomes empty after removal, it deletes the course from the object. The method returns true
if the student was successfully unenrolled and false
otherwise.
Next, let's implement the isEnrolled
method:
JavaScript1class EnrollmentSystem { 2 constructor() { 3 this.enrollments = {}; 4 } 5 6 enroll(student, course) { 7 if (!this.enrollments[course]) { 8 this.enrollments[course] = new Set(); 9 } 10 this.enrollments[course].add(student); 11 } 12 13 unenroll(student, course) { 14 if (this.enrollments[course] && this.enrollments[course].has(student)) { 15 this.enrollments[course].delete(student); 16 if (this.enrollments[course].size === 0) { // If the set is empty, remove the course 17 delete this.enrollments[course]; 18 } 19 return true; 20 } 21 return false; 22 } 23 24 isEnrolled(student, course) { 25 return this.enrollments[course] && this.enrollments[course].has(student); 26 } 27} 28 29// Example usage: 30const es = new EnrollmentSystem(); 31es.enroll("Alice", "Math101"); 32console.log(es.isEnrolled("Alice", "Math101")); // Output: true 33console.log(es.isEnrolled("Bob", "Math101")); // Output: false
This method checks whether the specified course exists in the enrollments and whether the student is enrolled in that course. If both conditions are met, it returns true
; otherwise, it returns false
.
Finally, let's implement the listStudents
method:
JavaScript1class EnrollmentSystem { 2 constructor() { 3 this.enrollments = {}; 4 } 5 6 enroll(student, course) { 7 if (!this.enrollments[course]) { 8 this.enrollments[course] = new Set(); 9 } 10 this.enrollments[course].add(student); 11 } 12 13 unenroll(student, course) { 14 if (this.enrollments[course] && this.enrollments[course].has(student)) { 15 this.enrollments[course].delete(student); 16 if (this.enrollments[course].size === 0) { // If the set is empty, remove the course 17 delete this.enrollments[course]; 18 } 19 return true; 20 } 21 return false; 22 } 23 24 isEnrolled(student, course) { 25 return this.enrollments[course] && this.enrollments[course].has(student); 26 } 27 28 listStudents(course) { 29 if (this.enrollments[course]) { 30 return Array.from(this.enrollments[course]); 31 } 32 return []; 33 } 34} 35 36// Example usage: 37const es = new EnrollmentSystem(); 38es.enroll("Alice", "Math101"); 39es.enroll("Bob", "Math101"); 40console.log(es.listStudents("Math101")); // Output: ['Alice', 'Bob'] 41console.log(es.listStudents("Physics101")); // Output: []
The listStudents
method returns an array of students enrolled in the given course. If the course is not in the enrollments
object, it returns an empty array.
When using Array.from(<set>)
, the elements in the set are returned in the order of their insertion. This means that the array created by Array.from
will maintain the order in which students were added to the set.
For example:
- If
"Alice"
is enrolled in "Math101" first, followed by"Bob"
, the array will be['Alice', 'Bob']
. - If
"Bob"
was enrolled first, followed by"Alice"
, the array would be['Bob', 'Alice']
.
Hence, Array.from
preserves the order of elements as they were added to the Set
.
In today's lesson, we learned how to manage student enrollments using JavaScript Set
objects. We implemented methods to enroll and unenroll students, check enrollments, and list students in a course. This task provided a practical way to reinforce your understanding of sets in JavaScript.
I encourage you to move on to the practice to undertake similar challenges to deepen your understanding. Keep experimenting and honing your skills. Happy coding!