diff --git a/curriculum/challenges/english/08-coding-interview-prep/data-structures/create-and-add-to-sets-in-es6.english.md b/curriculum/challenges/english/08-coding-interview-prep/data-structures/create-and-add-to-sets-in-es6.english.md index e63c79373a..d88fa10d5b 100644 --- a/curriculum/challenges/english/08-coding-interview-prep/data-structures/create-and-add-to-sets-in-es6.english.md +++ b/curriculum/challenges/english/08-coding-interview-prep/data-structures/create-and-add-to-sets-in-es6.english.md @@ -30,7 +30,7 @@ For this exercise, return a set with the following values: 1, 2, 3, 'Taco' ```yml tests: - text: Your Set should only contain the values 1, 2, 3, Taco, Cat, Awesome. - testString: assert(function(){var test = checkSet(); return (test.size == 6) && test.has(1) && test.has(2) && test.has(3) && test.has('Taco') && test.has('Cat') && test.has('Awesome');}, 'Your Set should only contain the values 1, 2, 3, Taco, Cat, Awesome.'); + testString: assert((function(){var test = checkSet(); return (test.size == 6) && test.has(1) && test.has(2) && test.has(3) && test.has('Taco') && test.has('Cat') && test.has('Awesome');})(), 'Your Set should only contain the values 1, 2, 3, Taco, Cat, Awesome.'); ``` diff --git a/curriculum/challenges/english/08-coding-interview-prep/data-structures/perform-a-difference-on-two-sets-of-data.english.md b/curriculum/challenges/english/08-coding-interview-prep/data-structures/perform-a-difference-on-two-sets-of-data.english.md index fb3889b75a..c95267861d 100644 --- a/curriculum/challenges/english/08-coding-interview-prep/data-structures/perform-a-difference-on-two-sets-of-data.english.md +++ b/curriculum/challenges/english/08-coding-interview-prep/data-structures/perform-a-difference-on-two-sets-of-data.english.md @@ -21,9 +21,9 @@ For example, if setA = ['a','b','c'] and setB = ['a','b','d', ```yml tests: - text: Your Set class should have a difference method. - testString: assert(function(){var test = new Set(); return (typeof test.difference === 'function')}, 'Your Set class should have a difference method.'); + testString: assert((function(){var test = new Set(); return (typeof test.difference === 'function')})(), 'Your Set class should have a difference method.'); - text: The proper collection was returned - testString: assert(function(){var setA = new Set(); var setB = new Set(); setA.add('a'); setA.add('b'); setA.add('c'); setB.add('c'); setB.add('d'); var differenceSetAB = setA.difference(setB); return (differenceSetAB.size() === 2) && (differenceSetAB.values() === [ 'a', 'b' ])}, 'The proper collection was returned'); + testString: assert((function(){var setA = new Set(); var setB = new Set(); setA.add('a'); setA.add('b'); setA.add('c'); setB.add('c'); setB.add('d'); var differenceSetAB = setA.difference(setB); return (differenceSetAB.size() === 2) && DeepEqual(differenceSetAB.values(), [ 'a', 'b' ])})(), 'The proper collection was returned'); ``` @@ -107,7 +107,73 @@ 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;};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);});};} +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; + } +} ``` diff --git a/curriculum/challenges/english/08-coding-interview-prep/data-structures/perform-a-subset-check-on-two-sets-of-data.english.md b/curriculum/challenges/english/08-coding-interview-prep/data-structures/perform-a-subset-check-on-two-sets-of-data.english.md index 19ff2d7f4e..736180d15f 100644 --- a/curriculum/challenges/english/08-coding-interview-prep/data-structures/perform-a-subset-check-on-two-sets-of-data.english.md +++ b/curriculum/challenges/english/08-coding-interview-prep/data-structures/perform-a-subset-check-on-two-sets-of-data.english.md @@ -21,17 +21,17 @@ For example, if setA = ['a','b'] and setB = ['a','b','c','d'] ```yml tests: - text: Your Set class should have a union method. - testString: assert(function(){var test = new Set(); return (typeof test.subset === 'function')}, 'Your Set class should have a union method.'); + testString: assert((function(){var test = new Set(); return (typeof test.subset === 'function')})(), 'Your Set class should have a union method.'); - text: The first Set() was contained in the second Set - testString: assert(function(){var setA = new Set(); var setB = new Set(); setA.add('a'); setB.add('b'); setB.add('c'); setB.add('a'); setB.add('d'); var subsetSetAB = setA.subset(setB);return (subsetSetAB === true)}, 'The first Set() was contained in the second Set'); + testString: assert((function(){var setA = new Set(); var setB = new Set(); setA.add('a'); setB.add('b'); setB.add('c'); setB.add('a'); setB.add('d'); var subsetSetAB = setA.subset(setB);return (subsetSetAB === true)})(), 'The first Set() was contained in the second Set'); - text: ['a', 'b'].subset(['a', 'b', 'c', 'd']) should return true") - testString: assert(function(){var setA = new Set(); var setB = new Set(); setA.add('a'); setA.add('b'); setB.add('a'); setB.add('b'); setB.add('c'); setB.add('d'); var subsetSetAB = setA.subset(setB); return (subsetSetAB === true)}, "['a', 'b'].subset(['a', 'b', 'c', 'd']) should return true"); + testString: assert((function(){var setA = new Set(); var setB = new Set(); setA.add('a'); setA.add('b'); setB.add('a'); setB.add('b'); setB.add('c'); setB.add('d'); var subsetSetAB = setA.subset(setB); return (subsetSetAB === true)})(), "['a', 'b'].subset(['a', 'b', 'c', 'd']) should return true"); - text: ['a', 'b', 'c'].subset(['a', 'b']) should return false") - testString: assert(function(){var setA = new Set(); var setB = new Set(); setA.add('a'); setA.add('b'); setA.add('c'); setB.add('a'); setB.add('b'); var subsetSetAB = setA.subset(setB); return (subsetSetAB === false)}, "['a', 'b', 'c'].subset(['a', 'b']) should return false"); + testString: assert((function(){var setA = new Set(); var setB = new Set(); setA.add('a'); setA.add('b'); setA.add('c'); setB.add('a'); setB.add('b'); var subsetSetAB = setA.subset(setB); return (subsetSetAB === false)})(), "['a', 'b', 'c'].subset(['a', 'b']) should return false"); - text: [].subset([]) should return true - testString: assert(function(){var setA = new Set(); var setB = new Set(); var subsetSetAB = setA.subset(setB); return (subsetSetAB === true)}, '[].subset([]) should return true'); + testString: assert((function(){var setA = new Set(); var setB = new Set(); var subsetSetAB = setA.subset(setB); return (subsetSetAB === true)})(), '[].subset([]) should return true'); - text: ['a', 'b'].subset(['c', 'd']) should return false") - testString: assert(function(){var setA = new Set(); var setB = new Set(); setA.add('a'); setA.add('b'); setB.add('c'); setB.add('d'); var subsetSetAB = setA.subset(setB); return (subsetSetAB === false)}, "['a', 'b'].subset(['c', 'd']) should return false"); + testString: assert((function(){var setA = new Set(); var setB = new Set(); setA.add('a'); setA.add('b'); setB.add('c'); setB.add('d'); var subsetSetAB = setA.subset(setB); return (subsetSetAB === false)})(), "['a', 'b'].subset(['c', 'd']) should return false"); ``` diff --git a/curriculum/challenges/english/08-coding-interview-prep/data-structures/perform-an-intersection-on-two-sets-of-data.english.md b/curriculum/challenges/english/08-coding-interview-prep/data-structures/perform-an-intersection-on-two-sets-of-data.english.md index 8216b245ca..48c992b571 100644 --- a/curriculum/challenges/english/08-coding-interview-prep/data-structures/perform-an-intersection-on-two-sets-of-data.english.md +++ b/curriculum/challenges/english/08-coding-interview-prep/data-structures/perform-an-intersection-on-two-sets-of-data.english.md @@ -21,9 +21,9 @@ For example, if setA = ['a','b','c'] and setB = ['a','b','d', ```yml tests: - text: Your Set class should have a intersection method. - testString: assert(function(){var test = new Set(); return (typeof test.intersection === 'function')}, 'Your Set class should have a intersection method.'); + testString: assert((function(){var test = new Set(); return (typeof test.intersection === 'function')})(), 'Your Set class should have a intersection method.'); - text: The proper collection was returned - testString: assert(function(){ var setA = new Set(); var setB = new Set(); setA.add('a'); setA.add('b'); setA.add('c'); setB.add('c'); setB.add('d'); var intersectionSetAB = setA.intersection(setB); return (intersectionSetAB.size() === 1 && intersectionSetAB.values()[0] === 'c')}, 'The proper collection was returned'); + testString: assert((function(){ var setA = new Set(); var setB = new Set(); setA.add('a'); setA.add('b'); setA.add('c'); setB.add('c'); setB.add('d'); var intersectionSetAB = setA.intersection(setB); return (intersectionSetAB.size() === 1 && intersectionSetAB.values()[0] === 'c')})(), 'The proper collection was returned'); ``` @@ -96,7 +96,62 @@ 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;};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);});};} +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; + } +} ``` diff --git a/curriculum/challenges/english/08-coding-interview-prep/data-structures/remove-items-from-a-set-in-es6.english.md b/curriculum/challenges/english/08-coding-interview-prep/data-structures/remove-items-from-a-set-in-es6.english.md index ee96aa1756..85f54b115e 100644 --- a/curriculum/challenges/english/08-coding-interview-prep/data-structures/remove-items-from-a-set-in-es6.english.md +++ b/curriculum/challenges/english/08-coding-interview-prep/data-structures/remove-items-from-a-set-in-es6.english.md @@ -25,7 +25,7 @@ Now, create a set with the integers 1, 2, 3, 4, & 5. ```yml tests: - text: Your Set should contain the values 1, 3, & 4 - testString: assert(function(){var test = checkSet(); return test.has(1) && test.has(3) && test.has(4) && test.size === 3}, 'Your Set should contain the values 1, 3, & 4'); + testString: assert((function(){var test = checkSet(); return test.has(1) && test.has(3) && test.has(4) && test.size === 3;})(), 'Your Set should contain the values 1, 3, & 4'); ``` diff --git a/curriculum/challenges/english/08-coding-interview-prep/data-structures/use-.has-and-.size-on-an-es6-set.english.md b/curriculum/challenges/english/08-coding-interview-prep/data-structures/use-.has-and-.size-on-an-es6-set.english.md index 022faa88ee..610cef85d1 100644 --- a/curriculum/challenges/english/08-coding-interview-prep/data-structures/use-.has-and-.size-on-an-es6-set.english.md +++ b/curriculum/challenges/english/08-coding-interview-prep/data-structures/use-.has-and-.size-on-an-es6-set.english.md @@ -26,7 +26,7 @@ In this exercise we will pass an array and a value to the checkSet() function. Y ```yml tests: - text: checkSet([4, 5, 6], 3) should return [ false, 3 ] - testString: 'assert(function(){var test = checkSet([4,5,6], 3); test === [ false, 3 ]}, "checkSet([4, 5, 6], 3) should return [ false, 3 ]");' + testString: 'assert((function(){var test = checkSet([4,5,6], 3); return DeepEqual(test, [ false, 3 ]);})(), "checkSet([4, 5, 6], 3) should return [ false, 3 ]");' ``` diff --git a/curriculum/challenges/english/08-coding-interview-prep/data-structures/use-spread-and-notes-for-es5-set-integration.english.md b/curriculum/challenges/english/08-coding-interview-prep/data-structures/use-spread-and-notes-for-es5-set-integration.english.md index 7060446cae..ef4ffb9337 100644 --- a/curriculum/challenges/english/08-coding-interview-prep/data-structures/use-spread-and-notes-for-es5-set-integration.english.md +++ b/curriculum/challenges/english/08-coding-interview-prep/data-structures/use-spread-and-notes-for-es5-set-integration.english.md @@ -24,7 +24,7 @@ Now you've successfully learned how to use the ES6 Set() object, go ```yml tests: - text: Your Set was returned correctly! - testString: 'assert(function(){var test = checkSet(new Set([1,2,3,4,5,6,7])); test === [ 1, 2, 3, 4, 5, 6, 7 ]}, "Your Set was returned correctly!");' + testString: 'assert((function(){var test = checkSet(new Set([1,2,3,4,5,6,7])); return DeepEqual(test, [ 1, 2, 3, 4, 5, 6, 7 ]);})(), "Your Set was returned correctly!");' ```