-
Notifications
You must be signed in to change notification settings - Fork 2
/
evaluateExpr.js
executable file
·39 lines (37 loc) · 993 Bytes
/
evaluateExpr.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
/**
Evaluate expression
eg ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9
*/
var evaluate=function evaluate(tokens) {
var res=0;
var operators="+-*/";
var stack=[];
for (var i=0; i<tokens.length; i++) {
var t=tokens[i];
if (operators.indexOf(t)===-1) {
stack.push(t);
//console.log(stack);
}
else {
var a=parseInt(stack.pop(), 10);
var b=parseInt(stack.pop(), 10);
//console.log(t);
if (t==="+") {
stack.push(a+b);
}
else if (t==="-") {
stack.push(b-a);
}
else if (t==="*") {
stack.push(a*b);
}
else if (t==="/") {
stack.push(Math.floor(b/a));
}
}
}
res=stack.pop();
return res;
};
console.log(evaluate(["2", "1", "+", "3", "*"]));
console.log(evaluate(["4", "13", "5", "/", "+"]));