-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_with_class.html
More file actions
62 lines (59 loc) · 1.48 KB
/
Copy pathstack_with_class.html
File metadata and controls
62 lines (59 loc) · 1.48 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<h1>Stack with Class</h1>
<script>
class Stack {
item = [];
itemCount;
maxSize;
constructor(size) {
this.maxSize = size;
this.itemCount = this.item.length;
}
push(newValue) {
if (this.itemCount === this.maxSize) {
console.log("Stack is full");
return;
}
this.item[this.itemCount] = newValue;
this.itemCount++;
}
pop() {
if (this.itemCount === 0) {
console.log("Stack is empty");
return;
}
this.itemCount--;
let value = this.item[this.itemCount];
this.item.length = this.itemCount;
return value;
}
display() {
console.log(this.item);
}
reverseString(value) {
let reverersedValue = "";
for (let i = value.length - 1; i >= 0; i--) {
reverersedValue += value[i];
console.log(reverersedValue);
}
}
}
stack = new Stack(4);
stack.reverseString("Prakash");
// stack.push(50);
// stack.push(60);
// stack.push(70);
// stack.push(80);
// stack.push(90);
// stack.pop();
// stack.display();
</script>
</body>
</html>