From 69c4bfbfab9d4a2cb29064c718ef40a039bee724 Mon Sep 17 00:00:00 2001 From: MatMoul Date: Sat, 25 Apr 2026 03:41:01 +0200 Subject: [PATCH] fix: preserve stack until operation completes --- src/rpn-calculator.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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]);