var set = new Object ();В следующих нескольких упражнениях мы создадим полнофункциональный набор с нуля. Для этого упражнения создайте функцию, которая добавит значение в нашу коллекцию наборов, если это значение еще не существует в наборе. Например:
set.foo = true;
// Смотрите, существует ли foo в нашем наборе:
console.log (set.foo) // true
this.add = function (element) {Функция должна возвращать значение
// некоторый код для добавления значения к набору
}
true
если значение успешно добавлено, а false
противном случае.
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.
Set
class should have an add
method.
testString: assert((function(){var test = new Set(); return (typeof test.add === 'function')}()));
- 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)}()));
- 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);}()));
- 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);}()));
- text: Your Set
class should have a remove
method.
testString: assert((function(){var test = new Set(); return (typeof test.remove === 'function')}()));
- 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']);
- 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)}()));
- text: Your Set
class should have a size
method.
testString: assert((function(){var test = new Set(); return (typeof test.size === 'function')}()));
- 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)}()));
```