I'm creating my stack class. I followed a javascript data structures book but I changed some functions and I keep getting an error that says "s.length is not a function." I have a length function but I wonder since there is a keyword 'length' in javascript then having the same name as a function might be causing an issue.:
// LIFO
function Stack()
{
this.dataStore = [];
// top of the stack
this.top = 0;
this.push = push;
this.pop = pop;
this.peek = peek;
}
function push(element)
{
// when new element is pushed, it needs to be stored
// in the top position and top var needs to be incremented
// so the new top is the next empty pos in the array
//this.dataStore(this.top++) = element;
// the increment op after the call ensures that the
// current value of top is used to place the new element
// at the top of the stack before top is incremented
this.dataStore.push(element);
}
function pop()
{
// returns element in top pos of stack and then decrements
// the top variable
//return this.dataStore[--this.top];
return this.dataStore.pop(element);
}
function peek()
{
// returns the top element of the stack by accessing
// the element at the top-1 position of the array
//return this.dataStore[this.top-1];
return this.dataStore[items.length-1];
}
function length()
{
//return this.top;
return this.dataStore.length;
}
function clear()
{
//return this.top = 0;
return this.dataStore = [];
}
var s = new Stack();
s.push("David");
s.push("Raymond");
s.push("Bryan");
console.log("length: " + s.length());