var set = new Object ();Nos próximos exercícios, vamos construir um conjunto completo a partir do zero. Para este exercício, crie uma função que irá adicionar um valor à nossa coleção de conjuntos, desde que o valor ainda não exista no conjunto. Por exemplo:
set.foo = true;
// Veja se foo existe em nosso conjunto:
console.log (set.foo) // true
this.add = function (element) {A função deve retornar
// algum código para agregar valor ao conjunto
}
true
se o valor for adicionado com sucesso e, caso contrário, false
. Set
deve ter um método add
.
testString: 'assert((function(){var test = new Set(); return (typeof test.add === "function")}()), "Your Set
class should have an add
method.");'
- text: Seu método add
não deve adicionar valores duplicados.
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: Seu método add
deve retornar true
quando um valor for adicionado com sucesso.
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: Seu método add
deve retornar false
quando um valor duplicado é adicionado.
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.");'
```