diff --git a/src/rpn-calculator.js b/src/rpn-calculator.js index 106eed2..27a6055 100644 --- a/src/rpn-calculator.js +++ b/src/rpn-calculator.js @@ -369,9 +369,13 @@ class RpnCalculator { throw new Error('Stack underflow'); } - const operands = argCount > 0 ? this.stack.splice(0, argCount).reverse() : []; + const operands = argCount > 0 ? this.stack.slice(0, argCount).reverse() : []; const result = operation.execute(this, ...operands, ...args); + if (argCount > 0) { + this.stack.splice(0, argCount); + } + if (Array.isArray(result)) { for (let i = result.length - 1; i >= 0; i -= 1) { this.push(result[i]);