-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstackBasic.js
More file actions
41 lines (34 loc) · 867 Bytes
/
stackBasic.js
File metadata and controls
41 lines (34 loc) · 867 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
var stack = function(){
this.stackArray = [];
this.push = function(num){
this.stackArray.push(num);
}
this.pop = function(){
if(this.stackArray.length==0){
return "Error: No elements left in array";
}
return this.stackArray.pop();
}
this.peek = function(){
if(this.stackArray.length==0){
return "Error: No elements present in array";
}
return this.stackArray[this.stackArray.length-1];
}
this.isEmpty = function(){
if(this.stackArray.length==0){
return true;
}
return false;
}
this.printStack = function(){
console.log(this.stackArray);
}
}
var stackTest = new stack();
stackTest.stackArray = [1,2,4];
stackTest.push(5);
stackTest.printStack();
console.log("Peek: " + stackTest.peek());
console.log("IsEmpty: " + stackTest.isEmpty());
console.log("Popped: " + stackTest.pop());