UPDATE: I realized one thing I was doing wrong, in that I was sending an array as the parameter to the union method instead of sending a set. I’ve fixed that, but it’s still not passing the tests, even though I’m getting the exact result I’m supposed to get. Here’s the updated code (including the stuff that was already given):
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(newSet) {
let newArray = newSet.values();
for (let i = 0; i < newArray.length; i++) {
if (!this.has(newArray[i])) {
collection.push(newArray[i]);
}
}
return collection;
}
// change code above this line
}
let mySet = new Set();
mySet.add ("a");
mySet.add ("b");
mySet.add ("c");
let otherSet = new Set();
otherSet.add ("c");
otherSet.add ("d");;
console.log (mySet.union(otherSet));