# Perform a Union on Two Sets

**URL:** <https://forum.freecodecamp.org/t/perform-a-union-on-two-sets/208107>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [July 12, 2018, 3:23am UTC](https://forum.freecodecamp.org/t/perform-a-union-on-two-sets/208107 "2018-07-12T03:23:17Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidjor-hpan](https://avatars.discourse-cdn.com/v4/letter/d/90db22/32.png) [@davidjor-hpan](https://forum.freecodecamp.org/u/davidjor-hpan)\
**Post date:** [July 12, 2018, 3:23am UTC](https://forum.freecodecamp.org/t/perform-a-union-on-two-sets/208107/1 "2018-07-12T03:23:17Z")

</div>

My `union` function is not pass , it print error `final.indexOf is not a function` !!!

**Your code so far**

```js

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:**

> **[freeCodeCamp.org](https://www.freecodecamp.org/learn/coding-interview-prep/data-structures/perform-a-union-on-two-sets)**
>
> Learn to Code — For Free

---

<div class="post-metadata">

**Author:** ![alhazen1](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/alhazen1/32/85244_2.png) [@alhazen1](https://forum.freecodecamp.org/u/alhazen1)\
**Post date:** [July 12, 2018, 3:55am UTC](https://forum.freecodecamp.org/t/perform-a-union-on-two-sets/208107/2 "2018-07-12T03:55:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![davidjor-hpan](https://avatars.discourse-cdn.com/v4/letter/d/90db22/32.png) [@davidjor-hpan](https://forum.freecodecamp.org/u/davidjor-hpan)\
**Post date:** [July 12, 2018, 4:07am UTC](https://forum.freecodecamp.org/t/perform-a-union-on-two-sets/208107/3 "2018-07-12T04:07:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![alhazen1](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/alhazen1/32/85244_2.png) [@alhazen1](https://forum.freecodecamp.org/u/alhazen1)\
**Post date:** [July 12, 2018, 4:41am UTC](https://forum.freecodecamp.org/t/perform-a-union-on-two-sets/208107/4 "2018-07-12T04:41:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Edwinyms](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/edwinyms/32/102996_2.png) [@Edwinyms](https://forum.freecodecamp.org/u/Edwinyms)\
**Post date:** [November 24, 2018, 4:53am UTC](https://forum.freecodecamp.org/t/perform-a-union-on-two-sets/208107/5 "2018-11-24T04:53:13Z")

</div>

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

```auto
 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? 🙂 🙂 🙂

---

<div class="post-metadata">

**Author:** ![vdloc](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/vdloc/32/98430_2.png) [@vdloc](https://forum.freecodecamp.org/u/vdloc)\
**Post date:** [December 28, 2018, 5:58am UTC](https://forum.freecodecamp.org/t/perform-a-union-on-two-sets/208107/6 "2018-12-28T05:58:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![yangnana11](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/yangnana11/32/107780_2.png) [@yangnana11](https://forum.freecodecamp.org/u/yangnana11)\
**Post date:** [February 25, 2019, 2:19am UTC](https://forum.freecodecamp.org/t/perform-a-union-on-two-sets/208107/7 "2019-02-25T02:19:13Z")

</div>

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

```auto
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;
    };

```

---

<div class="post-metadata">

**Author:** ![SirRobLyon](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sirroblyon/32/108360_2.png) [@SirRobLyon](https://forum.freecodecamp.org/u/SirRobLyon)\
**Post date:** [March 6, 2019, 8:53am UTC](https://forum.freecodecamp.org/t/perform-a-union-on-two-sets/208107/8 "2019-03-06T08:53:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![h15200](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/h15200/32/122894_2.png) [@h15200](https://forum.freecodecamp.org/u/h15200)\
**Post date:** [April 9, 2020, 5:02pm UTC](https://forum.freecodecamp.org/t/perform-a-union-on-two-sets/208107/12 "2020-04-09T17:02:00Z")

</div>

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:

```auto
setA.union(setB) = ['a', 'b', 'c', 'd', 'e']

```

For the purpose of clarification, I would re-write this in your head as this

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

```

Good luck and cheers!

---

<div class="post-metadata">

**Author:** ![h15200](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/h15200/32/122894_2.png) [@h15200](https://forum.freecodecamp.org/u/h15200)\
**Post date:** [April 9, 2020, 5:38pm UTC](https://forum.freecodecamp.org/t/perform-a-union-on-two-sets/208107/13 "2020-04-09T17:38:13Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [February 2, 2021, 11:13am UTC](https://forum.freecodecamp.org/t/perform-a-union-on-two-sets/208107/14 "2021-02-02T11:13:42Z")

</div>


