Skip to content

Instantly share code, notes, and snippets.

@JordanMajd
Created April 6, 2016 21:42
Show Gist options
  • Save JordanMajd/8364d681a9b642f77c560cc61fc9b021 to your computer and use it in GitHub Desktop.
Save JordanMajd/8364d681a9b642f77c560cc61fc9b021 to your computer and use it in GitHub Desktop.
// Stack constructor
// properties:
// values: some way of storing data.
const Stack = function(){
// create the values property and assign it the value of an array literal.
this.values = [];
};
// pop
// description:
// removes and returns the value at the top of the stack.
// returns:
// the removed value.
Stack.prototype.pop = function(){
//if you push to add values use pop() to remove them.
return this.values.pop();
//if you unshift to add values to the stack use shift() to remove them.
//return this.values.shift();
};
// push
// parameters:
// value: the data to add to the stack.
// description:
// adds the value to the top of the stack.
Stack.prototype.push = function(value){
//if you pop() values off, use push to add values to the stack.
this.values.push(value);
//if you shift() values off, use push to add values to the stack.
this.values.shift();
};
// peek
// description:
// returns the value at the top of the stack
Stack.prototype.peek = function(){
//if you push/pop, show the last element in the array:
return this.values[this.values.length - 1];
//if you unshift/shif, show the first element in the array:
//return this.values[0];
};
// isEmpty
// description:
// returns true if the stack has no data in it
Stack.prototype.isEmpty = function(){
return this.values.length === 0;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment