Skip to content
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
NAMAN546
JavaScriptAlgorithms
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
JavaScriptAlgorithms
/
DataStructures
/
Stacks
/
postFixEvaluation.js
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
35 lines (30 loc) · 1.05 KB
master
Breadcrumbs
JavaScriptAlgorithms
/
DataStructures
/
Stacks
/
postFixEvaluation.js
Copy path
Top
File metadata and controls
Code
Blame
35 lines (30 loc) · 1.05 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
function
postFixEvaluation
(
expression
)
{
if
(
typeof
expression
!==
'string'
||
expression
.
length
===
0
)
{
throw
'Invalid post fix expression.'
;
}
var
stack
=
[
]
;
for
(
var
i
=
0
;
i
<
expression
.
length
;
i
++
)
{
var
curr
=
expression
[
i
]
;
if
(
curr
.
match
(
/
^
\d
$
/
)
)
{
stack
.
push
(
curr
-
'0'
)
;
}
else
if
(
curr
.
match
(
/
^
[
+
-
/
*
]
$
/
)
)
{
if
(
stack
.
length
<
2
)
{
throw
'Invalid post fix expression.'
;
}
var
op
=
{
'+'
:
function
(
a
,
b
)
{
return
a
+
b
}
,
'-'
:
function
(
a
,
b
)
{
return
b
-
a
}
,
'/'
:
function
(
a
,
b
)
{
return
b
/
a
}
,
'*'
:
function
(
a
,
b
)
{
return
a
*
b
}
}
;
var
first
=
stack
.
pop
(
)
;
var
second
=
stack
.
pop
(
)
;
stack
.
push
(
op
[
curr
]
(
first
,
second
)
)
;
}
else
{
throw
'Invalid post fix expression.'
;
}
}
return
stack
.
pop
(
)
;
}
console
.
log
(
postFixEvaluation
(
"235*+8-"
)
)
;
// === 9
console
.
log
(
postFixEvaluation
(
"23/"
)
)
;
// === 0.66
You can’t perform that action at this time.