2018-10-04 14:37:37 +01:00
---
id: 587d8250367417b2b2512c5f
title: Create a Stack Class
challengeType: 1
---
## Description
< section id = 'description' >
2019-07-18 17:32:12 +02:00
2018-10-04 14:37:37 +01:00
In the last section, we talked about what a stack is and how we can use an array to represent a stack. In this section, we will be creating our own stack class.
Although you can use arrays to create stacks, sometimes it is best to limit the amount of control we have with our stacks.
Apart from the < code > push< / code > and < code > pop< / code > method, stacks have other useful methods. Let's add a < code > peek< / code > , < code > isEmpty< / code > , and < code > clear< / code > method to our stack class.
< / section >
## Instructions
< section id = 'instructions' >
2019-07-18 17:32:12 +02:00
2019-06-20 13:16:31 -04:00
Write a < code > push< / code > method that pushes an element to the top of the stack, a < code > pop< / code > method that removes the element on the top of the stack, a < code > peek< / code > method that looks at the first element in the stack, an < code > isEmpty< / code > method that checks if the stack is empty, and a < code > clear< / code > method that removes all elements from the stack.
Normally stacks don't have this, but we've added a < code > print< / code > helper method that console logs the collection.
2018-10-04 14:37:37 +01:00
< / section >
## Tests
< section id = 'tests' >
```yml
tests:
- text: Your < code > Stack</ code > class should have a < code > push</ code > method.
2019-07-24 23:56:32 -07:00
testString: assert((function(){var test = new Stack(); return (typeof test.push === 'function')}()));
2018-10-04 14:37:37 +01:00
- text: Your < code > Stack</ code > class should have a < code > pop</ code > method.
2019-07-24 23:56:32 -07:00
testString: assert((function(){var test = new Stack(); return (typeof test.pop === 'function')}()));
2018-10-04 14:37:37 +01:00
- text: Your < code > Stack</ code > class should have a < code > peek</ code > method.
2019-07-24 23:56:32 -07:00
testString: assert((function(){var test = new Stack(); return (typeof test.peek === 'function')}()));
2018-10-04 14:37:37 +01:00
- text: Your < code > Stack</ code > class should have a < code > isEmpty</ code > method.
2019-07-24 23:56:32 -07:00
testString: assert((function(){var test = new Stack(); return (typeof test.isEmpty === 'function')}()));
2018-10-04 14:37:37 +01:00
- text: Your < code > Stack</ code > class should have a < code > clear</ code > method.
2019-07-24 23:56:32 -07:00
testString: assert((function(){var test = new Stack(); return (typeof test.clear === 'function')}()));
2018-10-04 14:37:37 +01:00
- text: The < code > peek</ code > method should return the top element of the stack
2019-07-24 23:56:32 -07:00
testString: assert((function(){var test = new Stack(); test.push('CS50'); return (test.peek() === 'CS50')}()));
2018-10-04 14:37:37 +01:00
- text: The < code > pop</ code > method should remove and return the top element of the stack
2019-07-24 23:56:32 -07:00
testString: assert((function(){var test = new Stack(); test.push('CS50'); return (test.pop() === 'CS50');}()));
2018-10-04 14:37:37 +01:00
- text: The < code > isEmpty</ code > method should return true if a stack does not contain any elements
2019-07-24 23:56:32 -07:00
testString: assert((function(){var test = new Stack(); return test.isEmpty()}()));
2018-10-04 14:37:37 +01:00
- text: The < code > clear</ code > method should remove all element from the stack
2019-07-24 23:56:32 -07:00
testString: assert((function(){var test = new Stack(); test.push('CS50'); test.clear(); return (test.isEmpty())}()));
2018-10-04 14:37:37 +01:00
```
2019-07-18 17:32:12 +02:00
2018-10-04 14:37:37 +01:00
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
2019-07-18 17:32:12 +02:00
2018-10-04 14:37:37 +01:00
< div id = 'js-seed' >
```js
2018-10-08 01:01:53 +01:00
function Stack() {
2019-06-20 13:16:31 -04:00
var collection = [];
this.print = function() {
console.log(collection);
};
// Only change code below this line
2018-10-04 14:37:37 +01:00
2019-06-20 13:16:31 -04:00
// Only change code above this line
2018-10-04 14:37:37 +01:00
}
```
2019-07-18 17:32:12 +02:00
2018-10-04 14:37:37 +01:00
< / div >
< / section >
## Solution
< section id = 'solution' >
```js
2018-11-12 09:32:11 +05:30
class Stack {
2019-06-20 13:16:31 -04:00
constructor() {
this.collection = [];
}
print() {
console.log(this.collection);
}
push(val) {
this.collection.push(val);
}
pop() {
return this.collection.pop();
}
peek() {
return this.collection[this.collection.length - 1];
}
isEmpty() {
return this.collection.length === 0;
}
clear() {
return (this.collection.length = 0);
}
2018-11-12 09:32:11 +05:30
}
2018-10-04 14:37:37 +01:00
```
2019-07-18 08:24:12 -07:00
2018-10-04 14:37:37 +01:00
< / section >