Data Structures - Perform a Union on Two Sets

Tell us what’s happening:
Describe your issue in detail here.

I am practicing javascript class here, please let me know what I am doing wrong here, the output is giving as expected and I think I have written the code as per the setup.

Your code so far

class Set {
  constructor() {
    // This will hold the set
    this.dictionary = {};
    this.length = 0;
  }
  // This method will check for the presence of an element and return true or false
  has(element) {
    return this.dictionary[element] !== undefined;
  }
  // This method will return all the values in the set
  values() {
    return Object.values(this.dictionary);
  }
  // This method will add an element to the set
  add(element) {
    if (!this.has(element)) {
      this.dictionary[element] = element;
      this.length++;
      return true;
    }

    return false;
  }
  // This method will remove an element from a set
  remove(element) {
    if (this.has(element)) {
      delete this.dictionary[element];
      this.length--;
      return true;
    }

    return false;
  }
  // This method will return the size of the set
  size() {
    return this.length;
  }
  // Only change code below this line
  union = function(){
        let a = setA.values();
        let b = setB.values();
      
        a.forEach(e => this.add(e));
        b.forEach(e => this.add(e));
        
        return this.values();
    }
  // Only change code above this line
}

let setA = new Set();
let setB = new Set()
setA.add(1);
setA.add(2);
setA.add(3);
console.log(setA.values());
setB.add(2);
setB.add(3);
setB.add(4);
setB.add(5);
console.log(setB.values());
const unionSet = new Set();
unionSet.union();
console.log(unionSet.union());

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36

Challenge: Data Structures - Perform a Union on Two Sets

Link to the challenge:

union = function(){

  1. This method should take another Set as an argument.

return this.values();

  1. should return a new Set

Happy coding :smiley:

  1. This method should take another Set as an argument.

another ‘Set’ means, I have to assume there is already a SetA present and another set will be SetB which I need to pass, right?

  1. should return a new Set

return this.values() is acually returning the new Set which is created by an Object calling the union method.

You need to test your case this way, like a OOP:

let setA = new Set();
let setB = new Set()
setA.add(1);
setA.add(2);
setA.add(3);
setB.add(2);
setB.add(3);
setB.add(4);
setB.add(5);
console.log(setA.union(setB));

Ok, now make sense… thanks i will try

 union = function(set){
        let a = this.values();
        let b = set.values();
        let unionSet = new Set();
        a.forEach(e => unionSet.add(e));
        b.forEach(e => unionSet.add(e));
        
        return unionSet.values();
    }

let setA = new Set();
let setB = new Set()
setA.add(1);
setA.add(2);
setA.add(3);
setB.add(2);
setB.add(3);
setB.add(4);
setB.add(5);
console.log(setA.union(setB));

Still not passing the test…

should return a new Set class.

should return a new Set class.

Yes, unionSet is new object I have created, the values from the unionSet is being return instead of modifing the values of either setA or setB.

you are pretty close, just that the test looking for a class type object:

1 Like

this is so stupid for me… but why??? expected answer is in array form
I just have to do this: return unionSet

And I do not understand the slang ‘class type object’; I try to get the output shown in your img.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.