Merges 3 Set Challenges (#34717)
* fix: merges 3 set challenges into 1 * fix: changed function to class syntax * fix: simplified instructions * fix: changed the example * style: minor formatting changes * style: formatted solution * fix: formatting
This commit is contained in:
@ -6,19 +6,23 @@ challengeType: 1
|
|||||||
|
|
||||||
## Description
|
## Description
|
||||||
<section id='description'>
|
<section id='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:
|
In this exercise we are going to create a class named <code>Set</code> 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.
|
||||||
<blockquote>var set = new Object();<br>set.foo = true;<br>// See if foo exists in our set:<br>console.log(set.foo) // true</blockquote>
|
We can see how ES6 set object works in the example below-
|
||||||
In the next few exercises, we will build a full featured Set from scratch.
|
<blockquote>const set1 = new Set([1, 2, 3, 5, 5, 2, 0]);<br>console.log(set1);<br>// output: {1, 2, 3, 5, 0}<br>console.log(set1.has(1));<br>// output: true<br>console.log(set1.has(6));<br>// output: false</blockquote>
|
||||||
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:
|
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.
|
||||||
<blockquote>this.add = function(element) {<br> //some code to add value to the set<br>}</blockquote>
|
Then we will create a remove method that removes a value from the set collection if it already exists.
|
||||||
The function should return <code>true</code> if the value is successfully added and <code>false</code> otherwise.
|
And finally, we will create a size method that returns the number of elements inside the set collection.
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
|
Create an <code>add</code> method that adds a unique value to the set collection and returns <code>true</code> if the value was successfully added and <code>false</code> otherwise.
|
||||||
|
|
||||||
|
Create a <code>remove</code> 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 <code>true</code>. Otherwise, it should return <code>false</code>.
|
||||||
|
Create a <code>size</code> method that returns the size of the set collection.
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
<section id='tests'>
|
<section id='tests'>
|
||||||
|
|
||||||
@ -32,6 +36,16 @@ tests:
|
|||||||
testString: assert((function(){var test = new Set(); var result = test.add('a'); return (result != undefined) && (result === true);}()), 'Your <code>add</code> method should return <code>true</code> when a value has been successfully added.');
|
testString: assert((function(){var test = new Set(); var result = test.add('a'); return (result != undefined) && (result === true);}()), 'Your <code>add</code> method should return <code>true</code> when a value has been successfully added.');
|
||||||
- text: Your <code>add</code> method should return <code>false</code> when a duplicate value is added.
|
- text: Your <code>add</code> method should return <code>false</code> 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 <code>add</code> method should return <code>false</code> 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 <code>add</code> method should return <code>false</code> when a duplicate value is added.');
|
||||||
|
- text: Your <code>Set</code> class should have a <code>remove</code> method.
|
||||||
|
testString: assert((function(){var test = new Set(); return (typeof test.remove === 'function')}()), 'Your <code>Set</code> class should have a <code>remove</code> method.');
|
||||||
|
- text: Your <code>remove</code> 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 <code>remove</code> method should only remove items that are present in the set.');
|
||||||
|
- text: Your <code>remove</code> 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 <code>remove</code> method should remove the given item from the set.');
|
||||||
|
- text: Your <code>Set</code> class should have a <code>size</code> method.
|
||||||
|
testString: assert((function(){var test = new Set(); return (typeof test.size === 'function')}()), 'Your <code>Set</code> class should have a <code>size</code> method.');
|
||||||
|
- text: The <code>size</code> 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 <code>size</code> method should return the number of elements in the collection.');
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -43,18 +57,27 @@ tests:
|
|||||||
<div id='js-seed'>
|
<div id='js-seed'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function Set() {
|
class Set {
|
||||||
// the var collection will hold our set
|
constructor() {
|
||||||
var collection = [];
|
// collection will hold our set
|
||||||
|
this.collection = [];
|
||||||
|
}
|
||||||
// this method will check for the presence of an element and return true or false
|
// this method will check for the presence of an element and return true or false
|
||||||
this.has = function(element) {
|
has(element) {
|
||||||
return (collection.indexOf(element) !== -1);
|
return this.collection.indexOf(element) !== -1;
|
||||||
};
|
}
|
||||||
// this method will return all the values in the set
|
// this method will return all the values in the set
|
||||||
this.values = function() {
|
values() {
|
||||||
return collection;
|
return this.collection;
|
||||||
};
|
}
|
||||||
// change code below this line
|
// 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
|
// change code above this line
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@ -62,7 +85,6 @@ function Set() {
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Solution
|
## Solution
|
||||||
@ -70,16 +92,34 @@ function Set() {
|
|||||||
|
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function Set() {
|
class Set {
|
||||||
var collection = [];
|
constructor() {
|
||||||
this.has = function(element) {
|
this.collection = [];
|
||||||
return (collection.indexOf(element) !== -1);
|
}
|
||||||
};
|
has(element) {
|
||||||
this.values = function() {
|
return this.collection.indexOf(element) !== -1;
|
||||||
return collection;
|
}
|
||||||
};
|
values() {
|
||||||
this.add = function(el) {
|
return this.collection;
|
||||||
return this.has(el) ? false : Boolean(collection.push(el));
|
}
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -1,99 +0,0 @@
|
|||||||
---
|
|
||||||
id: 587d8253367417b2b2512c6b
|
|
||||||
title: Remove from a Set
|
|
||||||
challengeType: 1
|
|
||||||
---
|
|
||||||
|
|
||||||
## Description
|
|
||||||
<section id='description'>
|
|
||||||
In this exercises we are going to create a delete function for our set.
|
|
||||||
</section>
|
|
||||||
|
|
||||||
## Instructions
|
|
||||||
<section id='instructions'>
|
|
||||||
The function should be named <code>this.remove</code>. 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 <code>true</code>. Otherwise, return <code>false</code>.
|
|
||||||
</section>
|
|
||||||
|
|
||||||
## Tests
|
|
||||||
<section id='tests'>
|
|
||||||
|
|
||||||
```yml
|
|
||||||
tests:
|
|
||||||
- text: Your <code>Set</code> class should have a <code>remove</code> method.
|
|
||||||
testString: assert((function(){var test = new Set(); return (typeof test.remove === 'function')}()), 'Your <code>Set</code> class should have a <code>remove</code> method.');
|
|
||||||
- text: Your <code>remove</code> 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 <code>remove</code> method should only remove items that are present in the set.');
|
|
||||||
- text: Your <code>remove</code> 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 <code>remove</code> method should remove the given item from the set.');
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
</section>
|
|
||||||
|
|
||||||
## Challenge Seed
|
|
||||||
<section id='challengeSeed'>
|
|
||||||
|
|
||||||
<div id='js-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
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</section>
|
|
||||||
|
|
||||||
## Solution
|
|
||||||
<section id='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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
</section>
|
|
@ -1,84 +0,0 @@
|
|||||||
---
|
|
||||||
id: 8d1923c8c441eddfaeb5bdef
|
|
||||||
title: Size of the Set
|
|
||||||
challengeType: 1
|
|
||||||
---
|
|
||||||
|
|
||||||
## Description
|
|
||||||
<section id='description'>
|
|
||||||
In this exercise we are going to create a size function for our Set. This function should be named <code>this.size</code> and it should return the size of the collection.
|
|
||||||
</section>
|
|
||||||
|
|
||||||
## Instructions
|
|
||||||
<section id='instructions'>
|
|
||||||
|
|
||||||
</section>
|
|
||||||
|
|
||||||
## Tests
|
|
||||||
<section id='tests'>
|
|
||||||
|
|
||||||
```yml
|
|
||||||
tests:
|
|
||||||
- text: Your <code>Set</code> class should have a <code>size</code> method.
|
|
||||||
testString: assert((function(){var test = new Set(); return (typeof test.size === 'function')}()), 'Your <code>Set</code> class should have a <code>size</code> method.');
|
|
||||||
- text: The <code>size</code> 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 <code>size</code> method should return the number of elements in the collection.');
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
</section>
|
|
||||||
|
|
||||||
## Challenge Seed
|
|
||||||
<section id='challengeSeed'>
|
|
||||||
|
|
||||||
<div id='js-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
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</section>
|
|
||||||
|
|
||||||
## Solution
|
|
||||||
<section id='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;};}
|
|
||||||
```
|
|
||||||
|
|
||||||
</section>
|
|
Reference in New Issue
Block a user