Remove items from a set in ES6

function checkSet(){
  // Only change code below this line
  var set = new Set([1,2,3,4,5]);
  set.add(1);
  set.add(2);
  set.add(3);
  set.add(4);
  set.add(5);

  set.delete(2);
  set.delete(5);




  // Only change code above this line
  return set;   
}

Error says, “set is not defined”. How do i fix this?

hi there, is this an fCC challenge? If yes, can you post the link to it?

It works for me. What browser are you using?

BTW, you do not need to add the numbers using add as you already have already created the Set with the numbers.

1 Like

Thanks for the reply. I am using the Chrome browser.

Hello! Thanks for the reply and Yes, its an FCC challenge. Here’s the link: https://www.freecodecamp.org/learn/coding-interview-prep/data-structures/remove-items-from-a-set-in-es6

(post deleted by author)

When I copied your code into the challenge, it worked for me. Could you try copying the code from here to the challenge? Maybe there was a formatting issue.

By the way, this also works:

function checkSet(){
  // Only change code below this line
  var set = new Set([1,2,3,4,5]);

  set.delete(2);
  set.delete(5);

  // Only change code above this line
  return set;   
}

As @lasjorg said, you do not need to add the numbers separately because you have already added them in the object constructor.

The only way I can get that error is if I do not declare the set variable, or try to access the variable outside the function.

Is the code you posted all the code you have in the editor and is it exactly the same?

@hbar1st , @lasjorg , @bvanb, thanks for your replies. I have followed your suggestions and now the code works. I really don’t know why my initial code didn’t work before. Even, adding the values straight from the constructor now works, but it didn’t before. Thanks.