Skip to content
This repository was archived by the owner on Dec 12, 2023. It is now read-only.

Commit fe61bea

Browse files
committed
Fixing #83.
1 parent 4e7bee1 commit fe61bea

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

stack-machine/stack-machine.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ module.exports = function (S) {
2121
try {
2222
for (var i = 0; i < S.length; i++) {
2323
if (S[i] === '+') {
24-
stack.push(addition.apply(null, stack.splice(-2)));
24+
stack.push(addition.call(null, stack.pop(), stack.pop()));
2525
} else if (S[i] === '*') {
26-
stack.push(multiply.apply(null, stack.splice(-2)));
26+
stack.push(multiply.call(null, stack.pop(), stack.pop()));
2727
} else {
2828
stack.push(+S[i]);
2929
}
@@ -32,5 +32,5 @@ module.exports = function (S) {
3232
return -1;
3333
}
3434

35-
return stack.pop() || -1;
35+
return stack.length ? stack.pop() : -1;
3636
}

test/javascript/stack-machine.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
var stackMachine = require('../../stack-machine/stack-machine'),
22
assert = require('assert');
33

4-
describe('stackMachine', function () {
4+
describe('stack machine', function () {
55
it('should correctly evaluate the two example problems', function () {
66
assert.equal(stackMachine('13+62*7+*'), 76);
77
assert.equal(stackMachine('11++'), -1);
88
});
99

1010
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++) {
1212
assert.equal(stackMachine(i.toString()), i,
1313
stackMachine(i.toString()) + ' == ' + i + '\n Input: ' + i.toString());
14+
}
1415
assert.equal(stackMachine('0123456789'), 9);
1516
assert.equal(stackMachine('123456*****0'), 0);
1617
});

0 commit comments

Comments
 (0)