diff --git a/curriculum/challenges/english/08-coding-interview-prep/data-structures/remove-from-a-set.english.md b/curriculum/challenges/english/08-coding-interview-prep/data-structures/remove-from-a-set.english.md index 5bba29e269..1a3759af2c 100644 --- a/curriculum/challenges/english/08-coding-interview-prep/data-structures/remove-from-a-set.english.md +++ b/curriculum/challenges/english/08-coding-interview-prep/data-structures/remove-from-a-set.english.md @@ -6,12 +6,12 @@ challengeType: 1 ## Description
-In this exercises we are going to create a delete function for our set. The function should be named this.remove. This function should accept a value and check if it exists in the set. If it does, remove that value from the set, and return true. Otherwise, return false. +In this exercises we are going to create a delete function for our set.
## Instructions
- +The function should be named this.remove. This function should accept a value and check if it exists in the set. If it does, remove that value from the set, and return true. Otherwise, return false.
## Tests @@ -71,7 +71,29 @@ function Set() { ```js -function Set() {var collection = []; this.has = function(e){return(collection.indexOf(e) !== -1);};this.values = function() {return collection;};this.add = function(element) {if (!this.has(element)) {collection.push(element);return true;} else {return false;}};this.remove = function(element) {if(this.has(element)) {var i = collection.indexOf(element);collection.splice(i, 1);return true;}return false;};} +function Set() { + var collection = []; + this.has = function(element) { + return (collection.indexOf(element) !== -1); + }; + this.values = function() { + return collection; + }; + this.add = function(element) { + if(!this.has(element)){ + collection.push(element); + return true; + } + return false; + }; + this.remove = function(element){ + if (this.has(element)){ + collection.splice(collection.indexOf(element), 1); + return true; + } + return false; + } +} ``` diff --git a/guide/english/certifications/coding-interview-prep/data-structures/remove-from-a-set/index.md b/guide/english/certifications/coding-interview-prep/data-structures/remove-from-a-set/index.md index ab4a2ec9d3..fc9cbdef35 100644 --- a/guide/english/certifications/coding-interview-prep/data-structures/remove-from-a-set/index.md +++ b/guide/english/certifications/coding-interview-prep/data-structures/remove-from-a-set/index.md @@ -3,8 +3,35 @@ title: Remove from a Set --- ## Remove from a Set -This is a stub. Help our community expand it. +### Method: +- 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. +### Solution: +```js +function Set() { + var collection = []; + this.has = function(element) { + return (collection.indexOf(element) !== -1); + }; + this.values = function() { + return collection; + }; + this.add = function(element) { + if(!this.has(element)){ + collection.push(element); + return true; + } + return false; + }; + this.remove = function(element){ + if (this.has(element)){ + collection.splice(collection.indexOf(element), 1); + return true; + } + return false; + } +} +``` -This quick style guide will help ensure your pull request gets accepted. - - +### Resources: +- [Wikipedia](https://en.wikipedia.org/wiki/Set_(abstract_data_type))