--- id: 587d8250367417b2b2512c5f title: Create a Stack Class challengeType: 1 videoUrl: '' localeTitle: Crie uma classe de pilha --- ## Description
Na última seção, falamos sobre o que é uma pilha e como podemos usar uma matriz para representar uma pilha. Nesta seção, criaremos nossa própria classe de pilha. Embora você possa usar matrizes para criar pilhas, às vezes é melhor limitar a quantidade de controle que temos com nossas pilhas. Além do método push e pop , as pilhas possuem outros métodos úteis. Vamos adicionar um método peek , isEmpty e clear a nossa classe stack. Instruções Escreva um método push que empurre um elemento para o topo da pilha, um método pop que remova o elemento na parte superior da pilha, um método de peek que examine o primeiro elemento na pilha, um método isEmpty que verifique se o elemento pilha está vazia e um método clear que remove todos os elementos da pilha. Normalmente as pilhas não têm isso, mas adicionamos um método auxiliar de print que o console registra na coleção.
## Instructions
## Tests
```yml tests: - text: Sua classe Stack deve ter um método push . testString: 'assert((function(){var test = new Stack(); return (typeof test.push === "function")}()), "Your Stack class should have a push method.");' - text: Sua classe Stack deve ter um método pop . testString: 'assert((function(){var test = new Stack(); return (typeof test.pop === "function")}()), "Your Stack class should have a pop method.");' - text: Sua classe Stack deve ter um método de peek . testString: 'assert((function(){var test = new Stack(); return (typeof test.peek === "function")}()), "Your Stack class should have a peek method.");' - text: Sua classe Stack deve ter um método isEmpty . testString: 'assert((function(){var test = new Stack(); return (typeof test.isEmpty === "function")}()), "Your Stack class should have a isEmpty method.");' - text: Sua classe Stack deve ter um método clear . testString: 'assert((function(){var test = new Stack(); return (typeof test.clear === "function")}()), "Your Stack class should have a clear method.");' - text: O método peek deve retornar o elemento principal da pilha testString: 'assert((function(){var test = new Stack(); test.push("CS50"); return (test.peek() === "CS50")}()), "The peek method should return the top element of the stack");' - text: O método pop deve remover e retornar o elemento principal da pilha testString: 'assert((function(){var test = new Stack(); test.push("CS50"); return (test.pop() === "CS50");}()), "The pop method should remove and return the top element of the stack");' - text: O método isEmpty deve retornar true se uma pilha não contiver nenhum elemento testString: 'assert((function(){var test = new Stack(); return test.isEmpty()}()), "The isEmpty method should return true if a stack does not contain any elements");' - text: O método clear deve remover todos os elementos da pilha testString: 'assert((function(){var test = new Stack(); test.push("CS50"); test.clear(); return (test.isEmpty())}()), "The clear method should remove all element from the stack");' ```
## Challenge Seed
```js function Stack() { var collection = []; this.print = function() { console.log(collection); }; // Only change code below this line // Only change code above this line } ```
## Solution
```js // solution required ```