4.0 KiB
4.0 KiB
id, title, challengeType
id | title | challengeType |
---|---|---|
587d8250367417b2b2512c5f | Create a Stack Class | 1 |
Description
push
and pop
method, stacks have other useful methods. Let's add a peek
, isEmpty
, and clear
method to our stack class.
Instructions
Write a push
method that pushes an element to the top of the stack, a pop
method that removes the element on the top of the stack, a peek
method that looks at the first element in the stack, an isEmpty
method that checks if the stack is empty, and a clear
method that removes all elements from the stack.
Normally stacks don't have this, but we've added a print
helper method that console logs the collection.
Instructions
Tests
tests:
- text: Your <code>Stack</code> class should have a <code>push</code> method.
testString: 'assert((function(){var test = new Stack(); return (typeof test.push === ''function'')}()), ''Your <code>Stack</code> class should have a <code>push</code> method.'');'
- text: Your <code>Stack</code> class should have a <code>pop</code> method.
testString: 'assert((function(){var test = new Stack(); return (typeof test.pop === ''function'')}()), ''Your <code>Stack</code> class should have a <code>pop</code> method.'');'
- text: Your <code>Stack</code> class should have a <code>peek</code> method.
testString: 'assert((function(){var test = new Stack(); return (typeof test.peek === ''function'')}()), ''Your <code>Stack</code> class should have a <code>peek</code> method.'');'
- text: Your <code>Stack</code> class should have a <code>isEmpty</code> method.
testString: 'assert((function(){var test = new Stack(); return (typeof test.isEmpty === ''function'')}()), ''Your <code>Stack</code> class should have a <code>isEmpty</code> method.'');'
- text: Your <code>Stack</code> class should have a <code>clear</code> method.
testString: 'assert((function(){var test = new Stack(); return (typeof test.clear === ''function'')}()), ''Your <code>Stack</code> class should have a <code>clear</code> method.'');'
- text: The <code>peek</code> method should return the top element of the stack
testString: 'assert((function(){var test = new Stack(); test.push(''CS50''); return (test.peek() === ''CS50'')}()), ''The <code>peek</code> method should return the top element of the stack'');'
- text: The <code>pop</code> method should remove and return the top element of the stack
testString: 'assert((function(){var test = new Stack(); test.push(''CS50''); return (test.pop() === ''CS50'');}()), ''The <code>pop</code> method should remove and return the top element of the stack'');'
- text: The <code>isEmpty</code> method should return true if a stack does not contain any elements
testString: 'assert((function(){var test = new Stack(); return test.isEmpty()}()), ''The <code>isEmpty</code> method should return true if a stack does not contain any elements'');'
- text: The <code>clear</code> method should remove all element from the stack
testString: 'assert((function(){var test = new Stack(); test.push(''CS50''); test.clear(); return (test.isEmpty())}()), ''The <code>clear</code> method should remove all element from the stack'');'
Challenge Seed
function Stack() {
var collection = [];
this.print = function() {
console.log(collection);
};
// Only change code below this line
// Only change code above this line
}
Solution
// solution required