diff --git a/curriculum/challenges/english/08-coding-interview-prep/data-structures/create-a-set-class.english.md b/curriculum/challenges/english/08-coding-interview-prep/data-structures/create-a-set-class.english.md index 6651a4e719..8b4546334f 100644 --- a/curriculum/challenges/english/08-coding-interview-prep/data-structures/create-a-set-class.english.md +++ b/curriculum/challenges/english/08-coding-interview-prep/data-structures/create-a-set-class.english.md @@ -6,19 +6,23 @@ challengeType: 1 ## Description
-In the next few exercises we are going to create a function to emulate a data structure called a "Set". A Set is like an array, but it cannot contain duplicate values. The typical use for a Set is to simply check for the presence of an item. This can be implemented with an object, for instance: -
var set = new Object();
set.foo = true;
// See if foo exists in our set:
console.log(set.foo) // true
-In the next few exercises, we will build a full featured Set from scratch. -For this exercise, create a function that will add a value to our set collection as long as the value does not already exist in the set. For example: -
this.add = function(element) {
//some code to add value to the set
}
-The function should return true if the value is successfully added and false otherwise. +In this exercise we are going to create a class named Set to emulate an abstract data structure called "set". A set is like an array, but it cannot contain duplicate values. The typical use for a set is to simply check for the presence of an item. +We can see how ES6 set object works in the example below- +
const set1 = new Set([1, 2, 3, 5, 5, 2, 0]);
console.log(set1);
// output: {1, 2, 3, 5, 0}
console.log(set1.has(1));
// output: true
console.log(set1.has(6));
// output: false
+First, we will create an add method that adds a value to our set collection as long as the value does not already exist in the set. +Then we will create a remove method that removes a value from the set collection if it already exists. +And finally, we will create a size method that returns the number of elements inside the set collection.
## Instructions
+Create an add method that adds a unique value to the set collection and returns true if the value was successfully added and false otherwise. +Create a remove method that accepts a value and checks if it exists in the set. If it does, then this method should remove it from the set collection, and return true. Otherwise, it should return false. +Create a size method that returns the size of the set collection.
+ ## Tests
@@ -32,6 +36,16 @@ tests: testString: assert((function(){var test = new Set(); var result = test.add('a'); return (result != undefined) && (result === true);}()), 'Your add method should return true when a value has been successfully added.'); - text: Your add method should return false when a duplicate value is added. testString: assert((function(){var test = new Set(); test.add('a'); var result = test.add('a'); return (result != undefined) && (result === false);}()), 'Your add method should return false when a duplicate value is added.'); + - text: Your Set class should have a remove method. + testString: assert((function(){var test = new Set(); return (typeof test.remove === 'function')}()), 'Your Set class should have a remove method.'); + - text: Your remove method should only remove items that are present in the set. + testString: assert.deepEqual((function(){var test = new Set(); test.add('a');test.add('b');test.remove('c'); return test.values(); })(), ['a', 'b'], 'Your remove method should only remove items that are present in the set.'); + - text: Your remove method should remove the given item from the set. + testString: assert((function(){var test = new Set(); test.add('a');test.add('b');test.remove('a'); var vals = test.values(); return (vals[0] === 'b' && vals.length === 1)}()), 'Your remove method should remove the given item from the set.'); + - text: Your Set class should have a size method. + testString: assert((function(){var test = new Set(); return (typeof test.size === 'function')}()), 'Your Set class should have a size method.'); + - text: The size method should return the number of elements in the collection. + testString: assert((function(){var test = new Set(); test.add('a');test.add('b');test.remove('a');return (test.size() === 1)}()), 'The size method should return the number of elements in the collection.'); ``` @@ -43,18 +57,27 @@ tests:
```js -function Set() { - // the var collection will hold our set - var collection = []; +class Set { + constructor() { + // collection will hold our set + this.collection = []; + } // this method will check for the presence of an element and return true or false - this.has = function(element) { - return (collection.indexOf(element) !== -1); - }; + has(element) { + return this.collection.indexOf(element) !== -1; + } // this method will return all the values in the set - this.values = function() { - return collection; - }; + values() { + return this.collection; + } // change code below this line + + // write your add method here + + // write your remove method here + + // write your size method here + // change code above this line } ``` @@ -62,7 +85,6 @@ function Set() {
-
## Solution @@ -70,17 +92,35 @@ function Set() { ```js -function Set() { - var collection = []; - this.has = function(element) { - return (collection.indexOf(element) !== -1); - }; - this.values = function() { - return collection; - }; - this.add = function(el) { - return this.has(el) ? false : Boolean(collection.push(el)); +class Set { + constructor() { + this.collection = []; + } + has(element) { + return this.collection.indexOf(element) !== -1; + } + values() { + return this.collection; + } + add(element) { + if (!this.has(element)) { + this.collection.push(element); + return true; + } else { + return false; } + } + remove(element) { + if (this.has(element)) { + let i = this.collection.indexOf(element); + this.collection.splice(i, 1); + return true; + } + return false; + } + size() { + return this.collection.length; + } } ``` 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 deleted file mode 100644 index 1a3759af2c..0000000000 --- a/curriculum/challenges/english/08-coding-interview-prep/data-structures/remove-from-a-set.english.md +++ /dev/null @@ -1,99 +0,0 @@ ---- -id: 587d8253367417b2b2512c6b -title: Remove from a Set -challengeType: 1 ---- - -## Description -
-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 -
- -```yml -tests: - - text: Your Set class should have a remove method. - testString: assert((function(){var test = new Set(); return (typeof test.remove === 'function')}()), 'Your Set class should have a remove method.'); - - text: Your remove method should only remove items that are present in the set. - testString: assert.deepEqual((function(){var test = new Set(); test.add('a');test.add('b');test.remove('c'); return test.values(); })(), ['a', 'b'], 'Your remove method should only remove items that are present in the set.'); - - text: Your remove method should remove the given item from the set. - testString: assert((function(){var test = new Set(); test.add('a');test.add('b');test.remove('a'); var vals = test.values(); return (vals[0] === 'b' && vals.length === 1)}()), 'Your remove method should remove the given item from the set.'); - -``` - -
- -## Challenge Seed -
- -
- -```js -function Set() { - // the var collection will hold the set - var collection = []; - // this method will check for the presence of an element and return true or false - this.has = function(element) { - return (collection.indexOf(element) !== -1); - }; - // this method will return all the values in the set - this.values = function() { - return collection; - }; - // this method will add an element to the set - this.add = function(element) { - if(!this.has(element)){ - collection.push(element); - return true; - } - return false; - }; - // change code below this line - // change code above this line -} -``` - -
- - - -
- -## 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; - } -} -``` - -
diff --git a/curriculum/challenges/english/08-coding-interview-prep/data-structures/size-of-the-set.english.md b/curriculum/challenges/english/08-coding-interview-prep/data-structures/size-of-the-set.english.md deleted file mode 100644 index cfdeeba9e8..0000000000 --- a/curriculum/challenges/english/08-coding-interview-prep/data-structures/size-of-the-set.english.md +++ /dev/null @@ -1,84 +0,0 @@ ---- -id: 8d1923c8c441eddfaeb5bdef -title: Size of the Set -challengeType: 1 ---- - -## Description -
-In this exercise we are going to create a size function for our Set. This function should be named this.size and it should return the size of the collection. -
- -## Instructions -
- -
- -## Tests -
- -```yml -tests: - - text: Your Set class should have a size method. - testString: assert((function(){var test = new Set(); return (typeof test.size === 'function')}()), 'Your Set class should have a size method.'); - - text: The size method should return the number of elements in the collection. - testString: assert((function(){var test = new Set(); test.add('a');test.add('b');test.remove('a');return (test.size() === 1)}()), 'The size method should return the number of elements in the collection.'); - -``` - -
- -## Challenge Seed -
- -
- -```js -function Set() { - // the var collection will hold the set - var collection = []; - // this method will check for the presence of an element and return true or false - this.has = function(element) { - return (collection.indexOf(element) !== -1); - }; - // this method will return all the values in the set - this.values = function() { - return collection; - }; - // this method will add an element to the set - this.add = function(element) { - if(!this.has(element)){ - collection.push(element); - return true; - } - return false; - }; - // this method will remove an element from a set - this.remove = function(element) { - if(this.has(element)){ - var index = collection.indexOf(element); - collection.splice(index,1); - return true; - } - return false; - }; - // change code below this line - // change code above this line -} -``` - -
- - - -
- -## Solution -
- - -```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;};this.size = function() {return collection.length;};} -``` - -