UPDATED: Data Structures: Perform a Union -- Can't Understand Why It's Not Passing

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));

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

Thank you for the instruction! I saw that the post came out looking ugly, but I wasn’t sure how to fix it.