Introduction

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!

Introducing Methods to Implement

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 returns true if the student was enrolled and has now been removed. Otherwise, it returns false. 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 returns true if the student is enrolled and false 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.

Step 1: Define the Class

We'll start by defining our class and then add each method one by one.

First, we define our EnrollmentSystem class:

class EnrollmentSystem {
    constructor() {
        this.enrollments = {};
    }
}

This code initializes an EnrollmentSystem class with an object named enrollments that maps courses to sets of students.

Step 2: Implement `enroll` Method

Next, we implement the enroll method:

class EnrollmentSystem {
    constructor() {
        this.enrollments = {};
    }

    enroll(student, course) {
        if (!this.enrollments[course]) {
            this.enrollments[course] = new Set();
        }
        this.enrollments[course].add(student);
    }
}

// Example usage:
const es = new EnrollmentSystem();
es.enroll("Alice", "Math101");
console.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:

  1. this.enrollments is an object that maps course names to sets of students.
  2. When es.enroll("Alice", "Math101") is called:
    • It checks if "Math101" exists in this.enrollments. Since it doesn't, a new Set is created for "Math101".
    • Alice is then added to the set for "Math101".

Thus, console.log(es.enrollments) outputs the following:

{
    Math101: Set(1) { 'Alice' }
}

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.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal