krahets/hello-algo · error · Error

стек пуст

Error message

стек пуст

What it means

Thrown by ArrayStack.pop (JS, array_stack.js) with message 'стек пуст' ('stack is empty') when popping from an empty stack. Backed by a plain JS array, the guard prevents relying on Array.pop's silent undefined return.

Source

Thrown at ru/codes/javascript/chapter_stack_and_queue/array_stack.js:31

    /* Получение длины стека */
    get size() {
        return this.#stack.length;
    }

    /* Проверка, пуст ли стек */
    isEmpty() {
        return this.#stack.length === 0;
    }

    /* Поместить в стек */
    push(num) {
        this.#stack.push(num);
    }

    /* Извлечь из стека */
    pop() {
        if (this.isEmpty()) throw new Error('стек пуст');
        return this.#stack.pop();
    }

    /* Доступ к верхнему элементу стека */
    top() {
        if (this.isEmpty()) throw new Error('стек пуст');
        return this.#stack[this.#stack.length - 1];
    }

    /* Вернуть Array */
    toArray() {
        return this.#stack;
    }
}

/* Driver Code */
/* Инициализация стека */
const stack = new ArrayStack();

View on GitHub (pinned to 69932aed18)

Solutions

  1. Check isEmpty() before pop: if (!stack.isEmpty()) stack.pop().
  2. In bracket/paren matching, treat empty-on-close as a mismatch rather than letting it throw.
  3. Return a sentinel when empty if your algorithm tolerates it.

Example fix

// before
while (stack.size() > 0) doX(stack.pop());
stack.pop(); // throws if size was miscounted

// after
while (!stack.isEmpty()) doX(stack.pop());
Defensive patterns

Strategy: validation

Validate before calling

if (!stack.isEmpty()) { const v = stack.pop(); }

Try / catch

try { stack.pop(); } catch (e) { if (e.message !== 'стек пуст') throw e; }

Prevention

When it happens

Trigger: Calling pop() when #stack.length === 0 (isEmpty() is true).

Common situations: Unbalanced push/pop pairs; DFS/recursion-simulation that over-pops; expression-evaluation (e.g., bracket matching) hitting a close with nothing open.

Related errors


AI-assisted analysis of krahets/hello-algo@69932aed18 (2026-08-13). Data as JSON: /api/errors/cf9e5db1b9cdc0e7. Report an issue: GitHub.