Skip to content

Instantly share code, notes, and snippets.

@Damimd10

Damimd10/stack.js

Last active Feb 9, 2019
Embed
What would you like to do?
function createStack() {
const stack = []
return {
push(item) {
stack.push(item)
},
pop() {
return stack.pop()
},
peek() {
return stack[stack.length - 1]
},
get length() {
return stack.length
},
isEmpty() {
return stack.length == 0
}
}
}
const lowerBodyStack = createStack();
lowerBodyStack.push('underwear')
lowerBodyStack.push('socks')
lowerBodyStack.push('pants')
lowerBodyStack.push('shoes')
lowerBodyStack.pop()
lowerBodyStack.peek() // pants
lowerBodyStack.length() // 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment