Next, we'll take a look at how to find out the elements two Sets have in common. This operation is called an intersection
of two sets. An element is in the intersection of two sets, if it exists in both of them.
In this recipe, we'll see how to create an intersection of two Set
instances.
This recipe assumes that you already have a workspace that allows you to create and run ES modules in your browser. If you don't, refer to the first two chapters.
- Open your command-line application, and navigate to your workspace.
- Create a new folder named
12-06-set-intersection
. - Create a
main.js
file that defines a new class namedRocket
that takes a constructor argument name and assigns it to an instance property:
// main.js class Rocket { constructor(name) { this.name = name; } }
- Create a function called
intersection
that takes twoset
arguments:
// main.js function intersection (set1, set2) {}
- Create a
result
set. Loop through...