Solution Review: Sets
Understand how to create and manage bounded Sets in JavaScript by using constructors with capacity and initial values. Learn to add elements conditionally, check for duplicates, and remove items while ensuring the set size respects defined limits. This lesson reinforces key concepts of inheritance and built-in Set methods.
We'll cover the following...
Solution
The solution to the challenge given in the previous lesson is given below.
Explanation
Let’s start the implementation of BoundedSet by defining the constructor.
Constructor
As you know from the object instantiation given at line 24 and line 38; there are two constructor parameters.
-
The first argument is an integer indicating the total number of elements a set can have, so we call it
capacity. -
The second argument is an array consisting of the array of values that we are calling
initialValues, ...