-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstackrecursive.js
More file actions
46 lines (41 loc) · 808 Bytes
/
stackrecursive.js
File metadata and controls
46 lines (41 loc) · 808 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
41
42
43
44
45
46
class Stack {
constructor() {
this.items = [];
this.anotherArr = [];
this.thirdArr = [];
}
getSize() {
return this.items.length;
}
isEmpty() {
return this.items.length === 0;
}
push(value) {
this.items.push(value);
}
middle() {
return Math.floor(this.items.length / 2);
}
middleEven() {
if (this.items[this.items.length - 1] % 2 === 0) {
this.anotherArr.push(this.items.pop());
} else {
this.thirdArr.push(this.items.pop());
}
if (this.isEmpty()) {
this.items.push(this.thirdArr);
return this.anotherArr;
}
return this.middleEven();
}
display() {
console.log(this.items);
}
}
const stack = new Stack();
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
stack.push(5);
stack.display();