Perform Union on Two Sets,need help

Tell us what’s happening:
I am getting this error:Cannot read property ‘values’ of undefined.Need help

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) {
            return true;
        return false;
   // this method will remove an element from a set
    this.remove = function(element) {
           var index = collection.indexOf(element);
            return true;
        return false;
    // this method will return the size of the set
    this.size = function() {
        return this.length;
    // change code below this line
        for(let i=0;i<arr2.length;i++){
            return this.values();
    // change code above this line

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36.

Link to the challenge:

I’m not 100% sure of that specific error message but I see a few things with your method that might be problems.

You are looping over arr2. Is arr2 really an array?
collection is an array. Does array have a method .has() ?
What data type are you returning? Or, is returning the result of this.values() what is expected?

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])) {
         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 me? :slight_smile:

Did you ever get an answer to this? I am getting the same error (‘final.indexOf is not a function’, but when I test my union function in the console, it works.

the challenge starts with a Set constructor that already knows how to create new instances, give it’s values, and knows how to handle adding new elements (ignoring duplicates), so @adarshk010 you have some duplicated logic where you could’ve reused what’s already implemented mainly adding elements.
the challenge asks you to:

  1. provide a new set (preferably keeping the original ones intact)
  2. return the new set containing all the elements without duplicates ( this explains the errors you are getting @adarshk010 @Edwinyms @JTorr)
    here’s a solution that satisties both (1) and (2):
this.union = function(otherSet) {
  // creating a new set (1)
  let newSet = new Set();
  // adding the values of the current set and the otherSet respectively (2)
  this.values().forEach(el => newSet.add(el));
  otherSet.values().forEach(el => newSet.add(el));
  return newSet;