My union function is not pass , it print error final.indexOf is not a function !!!
Your code so far
function Set() {
// the var collection will hold the set
var collection = [];
// this method will check for the presence of an element and return true or false
this.has = function(element) {
return (collection.indexOf(element) !== -1);
};
// this method will return all the values in the set
this.values = function() {
return collection;
};
// this method will add an element to the set
this.add = function(element) {
if(!this.has(element)){
collection.push(element);
return true;
}
return false;
};
// this method will remove an element from a set
this.remove = function(element) {
if(this.has(element)){
var index = collection.indexOf(element);
collection.splice(index,1);
return true;
}
return false;
};
// this method will return the size of the set
this.size = function() {
return collection.length;
};
// change code below this line
this.union = function(arr) {
for(var i = 0; i < arr.length; i++) {
if(!this.has(arr[i])) {
this.add(arr[i]);
}
}
return this.values();
}
// change code above this line
}
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.
Hi,
I’m not seeing it in your code but that means somewhere you have called .indexOf on a variable named final except final is not an array. Do you have a variable ‘final’?
Based on alhazen1 answer, you must create a new Set object which it has the values is union-ed values from 2 sets and return it instead than add the different values directly to the first Set.
Hoping this clears things up without spoiling the challenge too much
All references to the data type, ‘Set’ in this challenge (and some others) do not refer to the built in JS data type, Set, but the version of Set that we set up ourselves inside the challenges.
One other thing that might be a little misleading is this code block in the description:
setA.union(setB) = ['a', 'b', 'c', 'd', 'e']
For the purpose of clarification, I would re-write this in your head as this
Update: I just completed the next 2 challenges after this and the test case on the bottom left for those were a little misleading as well. These challenges should return a Set, NOT a collection!