1class Stack{
2 constructor()
3 {
4 this.items = [];
5 }
6
7 push(element)
8 {
9 // push element into the items
10 this.items.push(element);
11 }
12
13 pop()
14 {
15 if (this.items.length == 0)
16 return "Underflow";
17 return this.items.pop();
18 }
19
20 peek()
21 {
22 return this.items[this.items.length - 1];
23 }
24
25 printStack()
26 {
27 var str = "";
28 for (var i = 0; i < this.items.length; i++)
29 str += this.items[i] + " ";
30 return str;
31 }
32
33}
1function Stack() {
2 var collection = [];
3 this.print = function() {
4 console.log(collection);
5 };
6 this.push = function(val) {
7 return collection.push(val);
8 };
9 this.pop = function() {
10 return collection.pop();
11 };
12 this.peek = function() {
13 return collection[collection.length - 1];
14 };
15 this.isEmpty = function() {
16 return collection.length === 0;
17 };
18 this.clear = function() {
19 collection.length = 0;
20 };
21}
22