Can't pass tests for Set data structure

Tell us what’s happening:
I am unable to pass the tests even though my code seems alright . Please help me figure out what’s wrong.
Your code so far


class Set {
constructor() {
  // Dictionary will hold the items of our 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.indexOf(element) !== undefined;
}

// This method will return all the values in the set
values() {
  return this.dictionary;
}

// Only change code below this line
add(item){
  if(!~this.has(item)){
    this.dictionary.push(item);
    this.length++;
    return true;
  }
  return false;
}

remove(item){
  var index = this.dictionary.indexOf(item);
  if(~index){
    this.dictionary.splice(index,1);
    this.length--;
  }
}

size(){
  return this.length;
}
// Only change code above this line
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; rv:78.0) Gecko/20100101 Firefox/78.0.

Challenge: Create a Set Class

Link to the challenge:

Well, it looks like you modified the challenge.

You should only add the methods add, remove and size and don’t change the rest of the code.

So the dictionary is required to be an object, not an array. Btw. that’s why we also keep track of length. If we were to store dictionary as array, we could just get the length like this this.dictionary.length :wink:

Also, I’m not sure why you’re using ~ operator here. It’s enough if you use ! for negation.

if(!~this.has(item))

Tell us what’s happening:
I think I’ve written the code according to the given conditions but it seems to faill nearly all the tests.

Your code so far


class Set {
constructor() {
  // Dictionary will hold the items of our 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.indexOf(element) > -1;
}

// This method will return all the values in the set
values() {
  return Object.keys(this.dictionary);
}

add(item){
  if(!this.has(item)){
    dictionary.push(item);
    return true;
  }
  return false;
}

remove(item){
  var idx = this.dictionary.indexOf(item);
  if(idx === -1){
    return false;
  }else{
    return this.dictionary.splice(idx,1)[0];
  }
}

size(){
  return Object.keys(this.dictionary).length;
}
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; rv:78.0) Gecko/20100101 Firefox/78.0.

Challenge: Create a Set Class

Link to the challenge:

@sanadKadu hey,

You are trying to use an object as an array, methods like push wont work on objects.

This code below shouldn’t have been changed

it should be return this.dictionary[element] !== undefined;

To add an item to an object you can use brackets.

this.dictionary[item] = 'value'

To remove an item you can use the same method but add delete to the start.

delete this.dictionary[item]

Hope this helps :slight_smile:

@sanadKadu I have merged your threads. Please do not create duplicate threads for the same question. You can always post your latest code in the thread, just remember to format it.


When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

You should leave the methods already created for you as they are. They already work and you will use them in the methods that you will be creating.

Do not change or add anything that is not part of the requirements.

This was helpful. Thanks

so this means that the add() method should have two inputs? like value and item?
and are my conditionals ok?
like if i want to check whether the item already exists like !this.has(item) ?
and the size() method ?

@sanadKadu
yes if(!this.has(item)) for add function and if(this.has(item)) for remove function, and your size look good that should pass, and yes a value and item the value can be anything.

1 Like