This repository was archived by the owner on Dec 12, 2023. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +6
-5
lines changed Expand file tree Collapse file tree 2 files changed +6
-5
lines changed Original file line number Diff line number Diff line change @@ -21,9 +21,9 @@ module.exports = function (S) {
21
21
try {
22
22
for ( var i = 0 ; i < S . length ; i ++ ) {
23
23
if ( S [ i ] === '+' ) {
24
- stack . push ( addition . apply ( null , stack . splice ( - 2 ) ) ) ;
24
+ stack . push ( addition . call ( null , stack . pop ( ) , stack . pop ( ) ) ) ;
25
25
} else if ( S [ i ] === '*' ) {
26
- stack . push ( multiply . apply ( null , stack . splice ( - 2 ) ) ) ;
26
+ stack . push ( multiply . call ( null , stack . pop ( ) , stack . pop ( ) ) ) ;
27
27
} else {
28
28
stack . push ( + S [ i ] ) ;
29
29
}
@@ -32,5 +32,5 @@ module.exports = function (S) {
32
32
return - 1 ;
33
33
}
34
34
35
- return stack . pop ( ) || - 1 ;
35
+ return stack . length ? stack . pop ( ) : - 1 ;
36
36
}
Original file line number Diff line number Diff line change 1
1
var stackMachine = require ( '../../stack-machine/stack-machine' ) ,
2
2
assert = require ( 'assert' ) ;
3
3
4
- describe ( 'stackMachine ' , function ( ) {
4
+ describe ( 'stack machine ' , function ( ) {
5
5
it ( 'should correctly evaluate the two example problems' , function ( ) {
6
6
assert . equal ( stackMachine ( '13+62*7+*' ) , 76 ) ;
7
7
assert . equal ( stackMachine ( '11++' ) , - 1 ) ;
8
8
} ) ;
9
9
10
10
it ( 'should return the top number from the stack' , function ( ) {
11
- for ( var i = 0 ; i < 10 ; i ++ )
11
+ for ( var i = 0 ; i < 10 ; i ++ ) {
12
12
assert . equal ( stackMachine ( i . toString ( ) ) , i ,
13
13
stackMachine ( i . toString ( ) ) + ' == ' + i + '\n Input: ' + i . toString ( ) ) ;
14
+ }
14
15
assert . equal ( stackMachine ( '0123456789' ) , 9 ) ;
15
16
assert . equal ( stackMachine ( '123456*****0' ) , 0 ) ;
16
17
} ) ;
You can’t perform that action at this time.
0 commit comments