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: