Updated to add solution to "Remove from a Set" (#27500)
* Updated to add solution to "Remove from a Set" * fix: formatting * fix: formatting of code block
This commit is contained in:
committed by
Randell Dawson
parent
d157077337
commit
7187215068
@ -2,36 +2,56 @@
|
|||||||
title: Remove from a Set
|
title: Remove from a Set
|
||||||
---
|
---
|
||||||
## Remove from a Set
|
## Remove from a Set
|
||||||
|
 Remember to use <a>**`Read-Search-Ask`**</a> if you get stuck. Try to pair program  and write your own code 
|
||||||
|
|
||||||
### Method:
|
###  Problem Explanation:
|
||||||
- In this challenge we need to implement a `.remove()` method for the Set class we made in the previous challenge.
|
|
||||||
- The method should return `true` if the element was successfully removed and `false` otherwise.
|
To solve this problem, you have to write a function for the class that removes a given element from the array if the array contains the given element.
|
||||||
### Solution:
|
|
||||||
```js
|
|
||||||
function Set() {
|
##  Hint: 1
|
||||||
var collection = [];
|
|
||||||
this.has = function(element) {
|
|
||||||
return (collection.indexOf(element) !== -1);
|
Use the built-in "has" function already created to test if the element exists in the Set.
|
||||||
};
|
|
||||||
this.values = function() {
|
> _try to solve the problem now_
|
||||||
return collection;
|
|
||||||
};
|
##  Hint: 2
|
||||||
this.add = function(element) {
|
|
||||||
if(!this.has(element)){
|
Use the splice function to delete an element from an array.
|
||||||
collection.push(element);
|
|
||||||
return true;
|
> _try to solve the problem now_
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## Spoiler Alert!
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
**Solution ahead!**
|
||||||
|
|
||||||
|
##  Basic Code Solution:
|
||||||
|
|
||||||
|
this.remove = function(element){
|
||||||
|
if(this.has(element)) {
|
||||||
|
this.values().splice( this.values().indexOf(element), 1 );
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
};
|
|
||||||
this.remove = function(element){
|
|
||||||
if (this.has(element)){
|
|
||||||
collection.splice(collection.indexOf(element), 1);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### Resources:
|
 <a href='https://repl.it/repls/ReflectingUsedTrees' target='_blank' rel='nofollow'>Run Code</a>
|
||||||
- [Wikipedia](https://en.wikipedia.org/wiki/Set_(abstract_data_type))
|
|
||||||
|
### Code Explanation:
|
||||||
|
|
||||||
|
* The if statement checks if the collection contains the element.
|
||||||
|
* If the if statement evaluates to true, then starting from the index at which the element is located, one element is deleted.
|
||||||
|
* If the if statement evaluates to false, then nothing occurs.
|
||||||
|
|
||||||
|
##  NOTES FOR CONTRIBUTIONS:
|
||||||
|
|
||||||
|
 **DO NOT** add solutions that are similar to any existing solutions. If you think it is **_similar but better_**, then try to merge (or replace) the existing similar solution.
|
||||||
|
|
||||||
|
Add an explanation of your solution.
|
||||||
|
|
||||||
|
Categorize the solution in one of the following categories — **Basic**, **Intermediate** and **Advanced**. 
|
||||||
|
|
||||||
|
See  <a href='http://forum.freecodecamp.com/t/algorithm-article-template/14272' target='_blank' rel='nofollow'>**`Wiki Challenge Solution Template`**</a> for reference.
|
||||||
|
Reference in New Issue
Block a user