Perform a Union on Two Sets

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.

Link to the challenge:
https://learn.freecodecamp.org/coding-interview-prep/data-structures/perform-a-union-on-two-sets/

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’?

That’a all my code ! I never use final variable . Could it be a bug of question ?

Duh - I missed some things.

You are returning this.values() . The test is expecting you to return a Set object, not an array. this.values() returns an array.

Also, your function is based on an array parameter. The value actually passed to your function is also a Set, not an array.

Hi there. Since you have probably solved this, can you help me?

 this.union = function(insertedSet) {
        for(let i = 0; i < insertedSet.values().length; i++) {
            if(!this.has(insertedSet.values()[i])) {
                this.add(insertedSet.values()[i])
            }
        }
         return this.values()
    }

There’s apparently something wrong with my code since the console logs and error of ‘final.indexOf is not a function’. Can you please help? :slight_smile: :slight_smile: :slight_smile:

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.

1 Like

The return type is a Set Obj, and the parameter is a Set Obj

this.union = function(otherSet) {
        var unionSet = new Set();
        var firstSet = this.values();
        var secondSet = otherSet.values();
        firstSet.forEach(function(e){
            unionSet.add(e);
        });
        secondSet.forEach(function(e){
            unionSet.add(e);
        });
        return unionSet;
    };

Hmm is it just me or were the challenge requirements not specific enough here…

3 Likes

Yes I found this a little confusing as well!

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

setA.union(setB)  // returns setC whose collection is ['a', 'b', 'c', 'd', 'e']

Good luck and cheers!

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!