fix(learn): update solutions in interview-prep challenges (#38430)
Co-authored-by: Tom <20648924+moT01@users.noreply.github.com> Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
This commit is contained in:
@ -71,27 +71,31 @@ tests:
|
||||
|
||||
```js
|
||||
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
|
||||
has(element) {
|
||||
return this.collection.indexOf(element) !== -1;
|
||||
}
|
||||
// this method will return all the values in the set
|
||||
values() {
|
||||
return this.collection;
|
||||
}
|
||||
// change code below this line
|
||||
constructor() {
|
||||
// Dictionary will hold the items of our set
|
||||
this.dictionary = {};
|
||||
this.length = 0;
|
||||
}
|
||||
|
||||
// write your add method here
|
||||
// This method will check for the presence of an element and return true or false
|
||||
has(element) {
|
||||
return this.dictionary[element] !== undefined;
|
||||
}
|
||||
|
||||
// write your remove method here
|
||||
// This method will return all the values in the set as an array
|
||||
values() {
|
||||
return Object.keys(this.dictionary);
|
||||
}
|
||||
|
||||
// write your size method here
|
||||
// change code below this line
|
||||
|
||||
// write your add method here
|
||||
|
||||
// change code above this line
|
||||
// write your remove method here
|
||||
|
||||
// write your size method here
|
||||
|
||||
// change code above this line
|
||||
}
|
||||
```
|
||||
|
||||
@ -105,32 +109,40 @@ class Set {
|
||||
```js
|
||||
class Set {
|
||||
constructor() {
|
||||
this.collection = [];
|
||||
this.dictionary = {};
|
||||
this.length = 0;
|
||||
}
|
||||
|
||||
has(element) {
|
||||
return this.collection.indexOf(element) !== -1;
|
||||
return this.dictionary[element] !== undefined;
|
||||
}
|
||||
|
||||
values() {
|
||||
return this.collection;
|
||||
return Object.keys(this.dictionary);
|
||||
}
|
||||
|
||||
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);
|
||||
this.dictionary[element] = true;
|
||||
this.length++;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
remove(element) {
|
||||
if (this.has(element)) {
|
||||
delete this.dictionary[element];
|
||||
this.length--;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
size() {
|
||||
return this.collection.length;
|
||||
return this.length;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
@ -36,64 +36,80 @@ tests:
|
||||
<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;
|
||||
};
|
||||
// this method will return the size of the collection
|
||||
this.size = function() {
|
||||
return collection.length;
|
||||
};
|
||||
// this method will return the union of two sets
|
||||
this.union = function(otherSet) {
|
||||
var unionSet = new Set();
|
||||
var firstSet = this.values();
|
||||
var secondSet = otherSet.values();
|
||||
firstSet.forEach(function(e){
|
||||
unionSet.add(e);
|
||||
});
|
||||
secondSet.forEach(function(e){
|
||||
unionSet.add(e);
|
||||
});
|
||||
return unionSet;
|
||||
};
|
||||
// this method will return the intersection of two sets as a new set
|
||||
this.intersection = function(otherSet) {
|
||||
var intersectionSet = new Set();
|
||||
var firstSet = this.values();
|
||||
firstSet.forEach(function(e){
|
||||
if(otherSet.has(e)){
|
||||
intersectionSet.add(e);
|
||||
}
|
||||
});
|
||||
return intersectionSet;
|
||||
};
|
||||
// change code below this line
|
||||
// change code above this line
|
||||
class Set {
|
||||
constructor() {
|
||||
// This will hold the set
|
||||
this.dictionary = {};
|
||||
this.length = 0;
|
||||
}
|
||||
// this method will check for the presence of an element and return true or false
|
||||
has(element) {
|
||||
return this.dictionary[element] !== undefined;
|
||||
}
|
||||
// this method will return all the values in the set
|
||||
values() {
|
||||
return Object.keys(this.dictionary);
|
||||
}
|
||||
// this method will add an element to the set
|
||||
add(element) {
|
||||
if (!this.has(element)) {
|
||||
this.dictionary[element] = true;
|
||||
this.length++;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
// this method will remove an element from a set
|
||||
remove(element) {
|
||||
if (this.has(element)) {
|
||||
delete this.dictionary[element];
|
||||
this.length--;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
// this method will return the size of the set
|
||||
size() {
|
||||
return this.length;
|
||||
}
|
||||
// This is our union method from that lesson
|
||||
union(set) {
|
||||
const newSet = new Set();
|
||||
this.values().forEach(value => {
|
||||
newSet.add(value);
|
||||
})
|
||||
set.values().forEach(value => {
|
||||
newSet.add(value);
|
||||
})
|
||||
|
||||
return newSet;
|
||||
}
|
||||
// This is our intersection method from that lesson
|
||||
intersection(set) {
|
||||
const newSet = new Set();
|
||||
|
||||
let largeSet;
|
||||
let smallSet;
|
||||
if (this.dictionary.length > set.length) {
|
||||
largeSet = this;
|
||||
smallSet = set;
|
||||
} else {
|
||||
largeSet = set;
|
||||
smallSet = this;
|
||||
}
|
||||
|
||||
smallSet.values().forEach(value => {
|
||||
if (largeSet.dictionary[value]) {
|
||||
newSet.add(value);
|
||||
}
|
||||
})
|
||||
|
||||
return newSet;
|
||||
}
|
||||
// change code below this line
|
||||
// change code above this line
|
||||
}
|
||||
```
|
||||
|
||||
@ -104,72 +120,89 @@ function Set() {
|
||||
<section id='solution'>
|
||||
|
||||
```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;
|
||||
};
|
||||
// this method will return the size of the collection
|
||||
this.size = function() {
|
||||
return collection.length;
|
||||
};
|
||||
// this method will return the union of two sets
|
||||
this.union = function(otherSet) {
|
||||
var unionSet = new Set();
|
||||
var firstSet = this.values();
|
||||
var secondSet = otherSet.values();
|
||||
firstSet.forEach(function(e){
|
||||
unionSet.add(e);
|
||||
});
|
||||
secondSet.forEach(function(e){
|
||||
unionSet.add(e);
|
||||
});
|
||||
return unionSet;
|
||||
};
|
||||
// this method will return the intersection of two sets as a new set
|
||||
this.intersection = function(otherSet) {
|
||||
var intersectionSet = new Set();
|
||||
var firstSet = this.values();
|
||||
firstSet.forEach(function(e){
|
||||
if(otherSet.has(e)){
|
||||
intersectionSet.add(e);
|
||||
}
|
||||
});
|
||||
return intersectionSet;
|
||||
};
|
||||
this.difference = function(otherSet) {
|
||||
var differenceSet = new Set();
|
||||
var firstSet = this.values();
|
||||
firstSet.forEach(function(e) {
|
||||
if (!otherSet.has(e)) {
|
||||
differenceSet.add(e);
|
||||
}
|
||||
});
|
||||
return differenceSet;
|
||||
class Set {
|
||||
constructor() {
|
||||
this.dictionary = {};
|
||||
this.length = 0;
|
||||
}
|
||||
|
||||
has(element) {
|
||||
return this.dictionary[element] !== undefined;
|
||||
}
|
||||
|
||||
values() {
|
||||
return Object.keys(this.dictionary);
|
||||
}
|
||||
|
||||
add(element) {
|
||||
if (!this.has(element)) {
|
||||
this.dictionary[element] = true;
|
||||
this.length++;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
remove(element) {
|
||||
if (this.has(element)) {
|
||||
delete this.dictionary[element];
|
||||
this.length--;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
size() {
|
||||
return this.length;
|
||||
}
|
||||
|
||||
union(set) {
|
||||
const newSet = new Set();
|
||||
this.values().forEach(value => {
|
||||
newSet.add(value);
|
||||
})
|
||||
set.values().forEach(value => {
|
||||
newSet.add(value);
|
||||
})
|
||||
|
||||
return newSet;
|
||||
}
|
||||
|
||||
intersection(set) {
|
||||
const newSet = new Set();
|
||||
|
||||
let largeSet;
|
||||
let smallSet;
|
||||
if (this.dictionary.length > set.length) {
|
||||
largeSet = this;
|
||||
smallSet = set;
|
||||
} else {
|
||||
largeSet = set;
|
||||
smallSet = this;
|
||||
}
|
||||
|
||||
smallSet.values().forEach(value => {
|
||||
if (largeSet.dictionary[value]) {
|
||||
newSet.add(value);
|
||||
}
|
||||
})
|
||||
|
||||
return newSet;
|
||||
}
|
||||
|
||||
difference(set) {
|
||||
const newSet = new Set();
|
||||
|
||||
this.values().forEach(value => {
|
||||
if (!set.dictionary[value]) {
|
||||
newSet.add(value);
|
||||
}
|
||||
})
|
||||
|
||||
return newSet;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -43,75 +43,92 @@ tests:
|
||||
<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;
|
||||
};
|
||||
// this method will return the size of the collection
|
||||
this.size = function() {
|
||||
return collection.length;
|
||||
};
|
||||
// this method will return the union of two sets
|
||||
this.union = function(otherSet) {
|
||||
var unionSet = new Set();
|
||||
var firstSet = this.values();
|
||||
var secondSet = otherSet.values();
|
||||
firstSet.forEach(function(e){
|
||||
unionSet.add(e);
|
||||
});
|
||||
secondSet.forEach(function(e){
|
||||
unionSet.add(e);
|
||||
});
|
||||
return unionSet;
|
||||
};
|
||||
// this method will return the intersection of two sets as a new set
|
||||
this.intersection = function(otherSet) {
|
||||
var intersectionSet = new Set();
|
||||
var firstSet = this.values();
|
||||
firstSet.forEach(function(e){
|
||||
if(otherSet.has(e)){
|
||||
intersectionSet.add(e);
|
||||
}
|
||||
});
|
||||
return intersectionSet;
|
||||
};
|
||||
// this method will return the difference of two sets as a new set
|
||||
this.difference = function(otherSet) {
|
||||
var differenceSet = new Set();
|
||||
var firstSet = this.values();
|
||||
firstSet.forEach(function(e){
|
||||
if(!otherSet.has(e)){
|
||||
differenceSet.add(e);
|
||||
}
|
||||
});
|
||||
return differenceSet;
|
||||
};
|
||||
// change code below this line
|
||||
// change code above this line
|
||||
class Set {
|
||||
constructor() {
|
||||
// This will hold the set
|
||||
this.dictionary = {};
|
||||
this.length = 0;
|
||||
}
|
||||
// this method will check for the presence of an element and return true or false
|
||||
has(element) {
|
||||
return this.dictionary[element] !== undefined;
|
||||
}
|
||||
// this method will return all the values in the set
|
||||
values() {
|
||||
return Object.keys(this.dictionary);
|
||||
}
|
||||
// this method will add an element to the set
|
||||
add(element) {
|
||||
if (!this.has(element)) {
|
||||
this.dictionary[element] = true;
|
||||
this.length++;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
// this method will remove an element from a set
|
||||
remove(element) {
|
||||
if (this.has(element)) {
|
||||
delete this.dictionary[element];
|
||||
this.length--;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
// this method will return the size of the set
|
||||
size() {
|
||||
return this.length;
|
||||
}
|
||||
// This is our union method from that lesson
|
||||
union(set) {
|
||||
const newSet = new Set();
|
||||
this.values().forEach(value => {
|
||||
newSet.add(value);
|
||||
})
|
||||
set.values().forEach(value => {
|
||||
newSet.add(value);
|
||||
})
|
||||
|
||||
return newSet;
|
||||
}
|
||||
// This is our intersection method from that lesson
|
||||
intersection(set) {
|
||||
const newSet = new Set();
|
||||
|
||||
let largeSet;
|
||||
let smallSet;
|
||||
if (this.dictionary.length > set.length) {
|
||||
largeSet = this;
|
||||
smallSet = set;
|
||||
} else {
|
||||
largeSet = set;
|
||||
smallSet = this;
|
||||
}
|
||||
|
||||
smallSet.values().forEach(value => {
|
||||
if (largeSet.dictionary[value]) {
|
||||
newSet.add(value);
|
||||
}
|
||||
})
|
||||
|
||||
return newSet;
|
||||
}
|
||||
// This is our difference method from that lesson
|
||||
difference(set) {
|
||||
const newSet = new Set();
|
||||
|
||||
this.values().forEach(value => {
|
||||
if (!set.dictionary[value]) {
|
||||
newSet.add(value);
|
||||
}
|
||||
})
|
||||
|
||||
return newSet;
|
||||
}
|
||||
// change code below this line
|
||||
// change code above this line
|
||||
}
|
||||
```
|
||||
|
||||
@ -122,7 +139,97 @@ function Set() {
|
||||
<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;};this.union = function(set) {var u = new Set();var c = this.values();var s = set.values();c.forEach(function(element){u.add(element);});s.forEach(function(element){u.add(element);});return u;};this.intersection = function(set) {var i = new Set();var c = this.values();c.forEach(function(element){if(s.has(element)) i.add(element);});};this.difference = function(set) {var d = new Set();var c = this.values();c.forEach(function(e){if(!set.has(e)) d.add(e);});};this.subset = function(set) {var isSubset = true;var c = this.values();c.forEach(function(e){if(!set.has(e)) isSubset = false;});return isSubset;};}
|
||||
class Set {
|
||||
constructor() {
|
||||
this.dictionary = {};
|
||||
this.length = 0;
|
||||
}
|
||||
|
||||
has(element) {
|
||||
return this.dictionary[element] !== undefined;
|
||||
}
|
||||
|
||||
values() {
|
||||
return Object.keys(this.dictionary);
|
||||
}
|
||||
|
||||
add(element) {
|
||||
if (!this.has(element)) {
|
||||
this.dictionary[element] = true;
|
||||
this.length++;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
remove(element) {
|
||||
if (this.has(element)) {
|
||||
delete this.dictionary[element];
|
||||
this.length--;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
size() {
|
||||
return this.length;
|
||||
}
|
||||
|
||||
union(set) {
|
||||
const newSet = new Set();
|
||||
this.values().forEach(value => {
|
||||
newSet.add(value);
|
||||
})
|
||||
set.values().forEach(value => {
|
||||
newSet.add(value);
|
||||
})
|
||||
|
||||
return newSet;
|
||||
}
|
||||
|
||||
intersection(set) {
|
||||
const newSet = new Set();
|
||||
|
||||
let largeSet;
|
||||
let smallSet;
|
||||
if (this.dictionary.length > set.length) {
|
||||
largeSet = this;
|
||||
smallSet = set;
|
||||
} else {
|
||||
largeSet = set;
|
||||
smallSet = this;
|
||||
}
|
||||
|
||||
smallSet.values().forEach(value => {
|
||||
if (largeSet.dictionary[value]) {
|
||||
newSet.add(value);
|
||||
}
|
||||
})
|
||||
|
||||
return newSet;
|
||||
}
|
||||
|
||||
difference(set) {
|
||||
const newSet = new Set();
|
||||
|
||||
this.values().forEach(value => {
|
||||
if (!set.dictionary[value]) {
|
||||
newSet.add(value);
|
||||
}
|
||||
})
|
||||
|
||||
return newSet;
|
||||
}
|
||||
|
||||
subset(set) {
|
||||
for(const value of this.values()){
|
||||
if(!set.dictionary[value]) return false;
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -34,41 +34,47 @@ tests:
|
||||
<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;
|
||||
};
|
||||
// this method will return the size of the set
|
||||
this.size = function() {
|
||||
return collection.length;
|
||||
};
|
||||
// change code below this line
|
||||
class Set {
|
||||
constructor() {
|
||||
// This will hold the set
|
||||
this.dictionary = {};
|
||||
this.length = 0;
|
||||
}
|
||||
// this method will check for the presence of an element and return true or false
|
||||
has(element) {
|
||||
return this.dictionary[element] !== undefined;
|
||||
}
|
||||
// this method will return all the values in the set
|
||||
values() {
|
||||
return Object.keys(this.dictionary);
|
||||
}
|
||||
// this method will add an element to the set
|
||||
add(element) {
|
||||
if (!this.has(element)) {
|
||||
this.dictionary[element] = true;
|
||||
this.length++;
|
||||
return true;
|
||||
}
|
||||
|
||||
// change code above this line
|
||||
return false;
|
||||
}
|
||||
// this method will remove an element from a set
|
||||
remove(element) {
|
||||
if (this.has(element)) {
|
||||
delete this.dictionary[element];
|
||||
this.length--;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
// this method will return the size of the set
|
||||
size() {
|
||||
return this.length;
|
||||
}
|
||||
// change code below this line
|
||||
|
||||
// change code above this line
|
||||
}
|
||||
```
|
||||
|
||||
@ -79,45 +85,55 @@ function Set() {
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
function Set() {
|
||||
var collection = [];
|
||||
class Set {
|
||||
constructor() {
|
||||
this.dictionary = {};
|
||||
this.length = 0;
|
||||
}
|
||||
|
||||
this.has = function(element) {
|
||||
return (collection.indexOf(element) !== -1);
|
||||
};
|
||||
has(element) {
|
||||
return this.dictionary[element] !== undefined;
|
||||
}
|
||||
|
||||
this.values = function() {
|
||||
return collection;
|
||||
};
|
||||
values() {
|
||||
return Object.keys(this.dictionary);
|
||||
}
|
||||
|
||||
this.add = function(element) {
|
||||
if(!this.has(element)){
|
||||
collection.push(element);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
add(element) {
|
||||
if (!this.has(element)) {
|
||||
this.dictionary[element] = true;
|
||||
this.length++;
|
||||
return true;
|
||||
}
|
||||
|
||||
this.remove = function(element) {
|
||||
if(this.has(element)){
|
||||
var index = collection.indexOf(element);
|
||||
collection.splice(index,1);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
return false;
|
||||
}
|
||||
|
||||
this.size = function() {
|
||||
return collection.length;
|
||||
};
|
||||
remove(element) {
|
||||
if (this.has(element)) {
|
||||
delete this.dictionary[element];
|
||||
this.length--;
|
||||
return true;
|
||||
}
|
||||
|
||||
this.union = function(anotherSet){
|
||||
const newSet = new Set();
|
||||
const addToSet = el => newSet.add(el);
|
||||
this.values().forEach(addToSet);
|
||||
anotherSet.values().forEach(addToSet);
|
||||
return newSet;
|
||||
};
|
||||
return false;
|
||||
}
|
||||
|
||||
size() {
|
||||
return this.length;
|
||||
}
|
||||
|
||||
union(set) {
|
||||
const newSet = new Set();
|
||||
this.values().forEach(value => {
|
||||
newSet.add(value);
|
||||
})
|
||||
set.values().forEach(value => {
|
||||
newSet.add(value);
|
||||
})
|
||||
|
||||
return newSet;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -35,53 +35,58 @@ tests:
|
||||
<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;
|
||||
};
|
||||
// this method will return the size of the collection
|
||||
this.size = function() {
|
||||
return collection.length;
|
||||
};
|
||||
// this method will return the union of two sets
|
||||
this.union = function(otherSet) {
|
||||
var unionSet = new Set();
|
||||
var firstSet = this.values();
|
||||
var secondSet = otherSet.values();
|
||||
firstSet.forEach(function(e){
|
||||
unionSet.add(e);
|
||||
});
|
||||
secondSet.forEach(function(e){
|
||||
unionSet.add(e);
|
||||
});
|
||||
return unionSet;
|
||||
};
|
||||
// change code below this line
|
||||
// change code above this line
|
||||
class Set {
|
||||
constructor() {
|
||||
// This will hold the set
|
||||
this.dictionary = {};
|
||||
this.length = 0;
|
||||
}
|
||||
// this method will check for the presence of an element and return true or false
|
||||
has(element) {
|
||||
return this.dictionary[element] !== undefined;
|
||||
}
|
||||
// this method will return all the values in the set
|
||||
values() {
|
||||
return Object.keys(this.dictionary);
|
||||
}
|
||||
// this method will add an element to the set
|
||||
add(element) {
|
||||
if (!this.has(element)) {
|
||||
this.dictionary[element] = true;
|
||||
this.length++;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
// this method will remove an element from a set
|
||||
remove(element) {
|
||||
if (this.has(element)) {
|
||||
delete this.dictionary[element];
|
||||
this.length--;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
// this method will return the size of the set
|
||||
size() {
|
||||
return this.length;
|
||||
}
|
||||
// This is our union method from that lesson
|
||||
union(set) {
|
||||
const newSet = new Set();
|
||||
this.values().forEach(value => {
|
||||
newSet.add(value);
|
||||
})
|
||||
set.values().forEach(value => {
|
||||
newSet.add(value);
|
||||
})
|
||||
|
||||
return newSet;
|
||||
}
|
||||
// change code below this line
|
||||
// change code above this line
|
||||
}
|
||||
```
|
||||
|
||||
@ -93,61 +98,77 @@ function Set() {
|
||||
|
||||
|
||||
```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;
|
||||
};
|
||||
// this method will return the size of the collection
|
||||
this.size = function() {
|
||||
return collection.length;
|
||||
};
|
||||
// this method will return the union of two sets
|
||||
this.union = function(otherSet) {
|
||||
var unionSet = new Set();
|
||||
var firstSet = this.values();
|
||||
var secondSet = otherSet.values();
|
||||
firstSet.forEach(function(e){
|
||||
unionSet.add(e);
|
||||
});
|
||||
secondSet.forEach(function(e){
|
||||
unionSet.add(e);
|
||||
});
|
||||
return unionSet;
|
||||
};
|
||||
this.intersection = function(otherSet) {
|
||||
var intersectionSet = new Set();
|
||||
var firstSet = this.values();
|
||||
firstSet.forEach(function(e) {
|
||||
if (otherSet.has(e)) {
|
||||
intersectionSet.add(e);
|
||||
}
|
||||
})
|
||||
return intersectionSet;
|
||||
class Set {
|
||||
constructor() {
|
||||
this.dictionary = {};
|
||||
this.length = 0;
|
||||
}
|
||||
|
||||
has(element) {
|
||||
return this.dictionary[element] !== undefined;
|
||||
}
|
||||
|
||||
values() {
|
||||
return Object.keys(this.dictionary);
|
||||
}
|
||||
|
||||
add(element) {
|
||||
if (!this.has(element)) {
|
||||
this.dictionary[element] = true;
|
||||
this.length++;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
remove(element) {
|
||||
if (this.has(element)) {
|
||||
delete this.dictionary[element];
|
||||
this.length--;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
size() {
|
||||
return this.length;
|
||||
}
|
||||
|
||||
union(set) {
|
||||
const newSet = new Set();
|
||||
this.values().forEach(value => {
|
||||
newSet.add(value);
|
||||
})
|
||||
set.values().forEach(value => {
|
||||
newSet.add(value);
|
||||
})
|
||||
|
||||
return newSet;
|
||||
}
|
||||
|
||||
intersection(set) {
|
||||
const newSet = new Set();
|
||||
|
||||
let largeSet;
|
||||
let smallSet;
|
||||
if (this.dictionary.length > set.length) {
|
||||
largeSet = this;
|
||||
smallSet = set;
|
||||
} else {
|
||||
largeSet = set;
|
||||
smallSet = this;
|
||||
}
|
||||
|
||||
smallSet.values().forEach(value => {
|
||||
if (largeSet.dictionary[value]) {
|
||||
newSet.add(value);
|
||||
}
|
||||
})
|
||||
|
||||
return newSet;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
Reference in New Issue
Block a user