Data Structures: Create a Set Class

Tell us what’s happening:

Hi Guys,

I am not sure why my code cannot pass the test. I tried to check it with som console logs and it seems

good to me:

[ 7, 4, 8 ]
3
succesfully removed
[ 4, 8 ]

Maybe I just miss something trivial or maybe is it some bug? Thanks

Your code so far


class Set {
constructor() {
// collection will hold our set
this.collection = [7];
}
// this method will check for the presence of an element and return true or false
has(element) {
    return this.collection.indexOf(element) !== -1;
}
// this method will return all the values in the set
values() {
    return this.collection;
}
// change code below this line

// write your add method here
add(el){
    if(!this.has(el)){
        this.collection.push(el);
        return true;
    }else{
        return false;
    }
}
// write your remove method here
remove(el){
    if(this.collection.indexOf(el) !== -1){
        this.collection.splice(this.collection.indexOf(el),1);
        return "succesfully removed"
    }
    return "no such item in collection"
}
// write your size method here
size(e){
    return this.collection.length
}
// change code above this line
}

var c = new Set;
c.add(7);
c.add(4);
c.add(7);
c.add(8);
console.log(c.values());
console.log(c.size());
console.log(c.remove(7));
console.log(c.values());

Your browser information:

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

Challenge: Create a Set Class

Link to the challenge:
https://www.freecodecamp.org/learn/coding-interview-prep/data-structures/create-a-set-class

What’s the purpose of number 7 in the array?

It was just for testing purpose if my add function add some duplicate or not.

But now I see that was the problem, thanks !