FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaScript-Interview-Question/Stack.js at master · namitmalasi/JavaScript-Interview-Question · GitHub
namitmalasi
/
JavaScript-Interview-Question
Public
Notifications
You must be signed in to change notification settings
Fork
5
Star
4
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaScript-Interview-Question
/
Stack.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
72 lines (63 loc) · 1.46 KB
Breadcrumbs
JavaScript-Interview-Question
/
Stack.js
Copy path
File metadata and controls
72 lines (63 loc) · 1.46 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
63
64
65
66
67
68
69
70
71
72
class
Stack
{
constructor
(
)
{
this
.
items
=
[
]
;
this
.
count
=
0
;
}
// Add element to top of stack
push
(
element
)
{
this
.
items
[
this
.
count
]
=
element
;
console
.
log
(
`
${
element
}
added to position
${
this
.
count
}
`
)
;
this
.
count
++
;
return
this
.
count
-
1
;
}
// Return and remove top element in stack
// Return undefined if stack is empty
pop
(
)
{
if
(
this
.
count
===
0
)
return
undefined
;
let
deletedItem
=
this
.
items
[
this
.
count
-
1
]
;
this
.
count
--
;
console
.
log
(
`
${
deletedItem
}
removed`
)
;
return
deletedItem
;
}
// Check top element in Stack
peek
(
)
{
console
.
log
(
`The top element is
${
this
.
items
[
this
.
count
-
1
]
}
`
)
;
let
peekedItem
=
this
.
items
[
this
.
count
-
1
]
;
return
peekedItem
;
}
// Check if stack is empty
isEmpty
(
)
{
console
.
log
(
this
.
count
==
0
?
"Stack is empty"
:
"Stack is not empty"
)
;
return
this
.
count
===
0
;
}
// Check size of Stack
size
(
)
{
console
.
log
(
`
${
this
.
count
}
element in stack `
)
;
return
this
.
count
;
}
// print elements of stack
print
(
)
{
let
str
=
""
;
for
(
let
i
=
0
;
i
<
this
.
count
;
i
++
)
{
str
=
str
+
this
.
items
[
i
]
+
" "
;
}
return
str
;
}
clear
(
)
{
this
.
items
=
[
]
;
this
.
count
=
0
;
console
.
log
(
"Stack cleared"
)
;
return
this
.
items
;
}
}
const
stack
=
new
Stack
(
)
;
stack
.
isEmpty
(
)
;
stack
.
push
(
10
)
;
stack
.
push
(
20
)
;
stack
.
push
(
30
)
;
stack
.
push
(
40
)
;
stack
.
pop
(
)
;
stack
.
pop
(
)
;
stack
.
isEmpty
(
)
;
stack
.
peek
(
)
;
console
.
log
(
stack
.
print
(
)
)
;
Back
|
FazBrowse Home
|
New Git URL