--- id: 8d1323c8c441eddfaeb5bdef title: Create a Set Class challengeType: 1 videoUrl: '' localeTitle: Criar uma classe de conjunto --- ## Description
Nos próximos exercícios, vamos criar uma função para emular uma estrutura de dados chamada "Set". Um conjunto é como uma matriz, mas não pode conter valores duplicados. O uso típico de um conjunto é simplesmente verificar a presença de um item. Isso pode ser implementado com um objeto, por exemplo:
var set = new Object ();
set.foo = true;
// Veja se foo existe em nosso conjunto:
console.log (set.foo) // true
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:
this.add = function (element) {
// algum código para agregar valor ao conjunto
}
A função deve retornar true se o valor for adicionado com sucesso e, caso contrário, false .
## Instructions
## Tests
```yml tests: - text: Sua classe 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.");' ```
## Challenge Seed
```js function Set() { // the var collection will hold our 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; }; // change code below this line // change code above this line } ```
## Solution
```js // solution required ```