--- id: 587d8250367417b2b2512c5f title: Create a Stack Class challengeType: 1 videoUrl: '' localeTitle: Crear una clase de pila --- ## Description
En la sección anterior, hablamos sobre qué es una pila y cómo podemos usar una matriz para representar una pila. En esta sección, crearemos nuestra propia clase de pila. Aunque puede utilizar matrices para crear pilas, a veces es mejor limitar la cantidad de control que tenemos con nuestras pilas. Aparte del método push y pop , las pilas tienen otros métodos útiles. isEmpty clear método peek , isEmpty y clear a nuestra clase de pila. Instrucciones Escriba un método de push que empuja un elemento a la parte superior de la pila, un método pop que elimina el elemento de la parte superior de la pila, un método de peek que mira el primer elemento de la pila, un método isEmpty que verifica si la pila está vacía, y un método clear que elimina todos los elementos de la pila. Normalmente las pilas no tienen esto, pero hemos agregado un método de ayuda de print que la consola registra la colección.
## Instructions
## Tests
```yml tests: - text: Tu clase de Stack debería tener un método push . testString: 'assert((function(){var test = new Stack(); return (typeof test.push === "function")}()), "Your Stack class should have a push method.");' - text: Tu clase de Stack debería tener un método pop . testString: 'assert((function(){var test = new Stack(); return (typeof test.pop === "function")}()), "Your Stack class should have a pop method.");' - text: Tu clase de Stack debería tener un 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: Tu clase de Stack debería tener un método isEmpty . testString: 'assert((function(){var test = new Stack(); return (typeof test.isEmpty === "function")}()), "Your Stack class should have a isEmpty method.");' - text: Tu clase de Stack debería tener un método clear . testString: 'assert((function(){var test = new Stack(); return (typeof test.clear === "function")}()), "Your Stack class should have a clear method.");' - text: El método peek debería devolver el elemento superior de la pila. 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: El método pop debería eliminar y devolver el elemento superior de la pila. 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: El método isEmpty debería devolver verdadero si una pila no contiene ningún 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: El método clear debe eliminar todos los elementos de la pila. 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 ```