Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 45 additions & 3 deletions spec/stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
})
})
})
26 changes: 25 additions & 1 deletion src/stack.js
Original file line number Diff line number Diff line change
@@ -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
}
}