diff --git a/spec/stack.js b/spec/stack.js index 743122a..a317f5f 100644 --- a/spec/stack.js +++ b/spec/stack.js @@ -13,10 +13,52 @@ describe('Stack', () => { context('push()', () => { it('pushes an element to the top of the stack.', () => { - const myStack = new Stack() + const stack = new Stack() - expect(() => myStack.push('foo')) - .to.alter(() => myStack.length(), { from: 0, to: 1 }) + expect(() => stack.push('foo')) + .to.alter(() => stack.length(), { from: 0, to: 1 }) + }) + + it('pushes an element to the top of the stack when there is preexisting data', () => { + const stack = new Stack() + stack.push('alice') + stack.push('bob') + + expect(() => stack.push('foo')) + .to.alter(() => stack.peek(), {from: 'bob', to: 'foo'}) + }) + + }) + context('pop()', () => { + it('pushes an element to the top of the stack.', () => { + const stack = new Stack() + + expect(() => stack.push('foo')) + .to.alter(() => stack.length(), { from: 0, to: 1 }) + }) + }) + context('peek()', () => { + it('pushes an element to the top of the stack.', () => { + const stack = new Stack() + + expect(() => stack.push('foo')) + .to.alter(() => stack.length(), { from: 0, to: 1 }) + }) + }) + context('isEmpty()', () => { + it('pushes an element to the top of the stack.', () => { + const stack = new Stack() + + expect(() => stack.push('foo')) + .to.alter(() => stack.length(), { from: 0, to: 1 }) + }) + }) + context('length()', () => { + it('pushes an element to the top of the stack.', () => { + const stack = new Stack() + + expect(() => stack.push('foo')) + .to.alter(() => stack.length(), { from: 0, to: 1 }) }) }) }) diff --git a/src/stack.js b/src/stack.js index dcd1d13..b503d40 100644 --- a/src/stack.js +++ b/src/stack.js @@ -1,5 +1,29 @@ 'use strict' export default class Stack { - // your code here + constructor(){ + this.size = 0 + this.value = [ ] + } + + push(element){ + this.value.unshift(element) + this.size++ + } + + pop(){ + return this.value.pop() + } + + peek(){ + return this.value[0] + } + + isEmpty(){ + return (this.size === 0) + } + + length(){ + return this.size + } }