-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.js
53 lines (41 loc) · 928 Bytes
/
stack.js
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
41
42
43
44
45
46
47
48
49
class Stack{
constructor(){
this.items = [];
}
push(item){
this.items.push(item);
}
pop(){
if(this.isEmpty()){
return 'Stack is empty'
}
this.items.pop()
}
isEmpty(){
return (this.items.length == 0) ? true: false;
}
printItems(){
let results = "";
for( let i = 0; i< this.items.length ; i++){
results += " "+ this.items[i] + "" ;
}
return results.trim();
}
}
let stack1 = new Stack();
stack1.push(2);
stack1.push(3);
stack1.push(4);
stack1.push(6);
console.log(stack1.printItems())
console.log(stack1.pop())
console.log(stack1.printItems())
stack1.push(7);
console.log(stack1.printItems())
console.log(stack1.pop())
console.log(stack1.pop())
console.log(stack1.pop())
console.log(stack1.pop())
console.log(stack1.pop())
console.log(stack1.pop())
console.log(stack1.pop())