Tell us what’s happening:
Can somebody please explain why the union method does not pass the tests? All the tests i’ve done returns the right result.
Your code so far
class Set {
constructor() {
this._collection = []
}
print() {
return console.log(this._collection)
}
add(item) {
if (this.has(item)) {
return false
}
this._collection = [...this._collection, item]
return true
}
remove(item) {
if(this.has(item)) {
this._collection = this._collection.filter(value => value !== item)
return true
}
return false
}
size() {
return this._collection.length
}
has(item) {
return this._collection.indexOf(item) !== -1
}
union(set) {
const setOnlyUniqueItems = set._collection.filter(item => !this.has(item))
this._collection = [...this._collection, ...setOnlyUniqueItems]
return this._collection
}
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36
.
Challenge: Perform a Union on Two Sets
Link to the challenge:
https://www.freecodecamp.org/learn/coding-interview-prep/data-structures/perform-a-union-on-two-sets