Exercise 1: Sets

Let's have some fun with the set implementation by doing this challenge.

Problem statement

Implement a BoundedSet that inherits from Set to achieve the desired behavior.

The object of the BoundedSet class is instantiated like so:

const set = new BoundedSet(5, ['Apple', 'Banana', 'Grape', 'Mango']);

Sample input and output

The set object is expected to behave such that it displays the following output on respective input calls.

Input Output
set.add('Orange'); set.add('Apple');
try { set.add('Tangerine'); } catch(ex) { console.log(ex.message);} exceeded capacity of 5 elements
set.delete('Grape'); set.add('Peach');
console.log(set.size); 5
console.log(set2.size); 0
console.log(set2.capacity); 2

Exercise

Complete this exercise to test your knowledge and skills.

Get hands-on with 1400+ tech skills courses.