var set = new Object();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:
set.foo = true;
// See if foo exists in our set:
console.log(set.foo) // true
this.add = function(element) {The function should return
//some code to add value to the set
}
true
if the value is successfully added and false
otherwise.
Set
class should have an add
method.
testString: 'assert((function(){var test = new Set(); return (typeof test.add === "function")}()), "Your Set
class should have an add
method.");'
- text: Your add
method should not add duplicate values.
testString: 'assert((function(){var test = new Set(); test.add("a"); test.add("b"); test.add("a"); var vals = test.values(); return (vals[0] === "a" && vals[1] === "b" && vals.length === 2)}()), "Your add
method should not add duplicate values.");'
- text: Your add
method should return true
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 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.");'
```