Solution Review: Injecting a Method
Here’s the solution to the injecting a method challenge given in the previous lesson.
We'll cover the following...
Solution
Let’s take a look at the solution code given below.
Press + to interact
'use strict';Set.prototype.combine = function(otherSet) {const copyOfSet = new Set(this);for(const element of otherSet) {copyOfSet.add(element);}return copyOfSet;};const names1 = new Set(['Tom', 'Sara', 'Brad', 'Kim']);const names2 = new Set(['Mike', 'Kate']);const combinedNames = names1.combine(names2);
Explanation
Let’s dive into the solution code.
First of all, we need to make a new Set
element in which there will be a combination of two sets. ...