{"record":{"id":"171735ce5681f794","repo":"krahets/hello-algo","slug":"error-171735","errorCode":null,"errorMessage":"очередь пуста","messagePattern":"очередь пуста","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"ru/codes/javascript/chapter_stack_and_queue/array_queue.js","lineNumber":57,"sourceCode":"        // С помощью операции взятия по модулю вернуть rear к началу после выхода за конец массива\n        const rear = (this.#front + this.size) % this.capacity;\n        // Добавить num в хвост очереди\n        this.#nums[rear] = num;\n        this.#queSize++;\n    }\n\n    /* Извлечь из очереди */\n    pop() {\n        const num = this.peek();\n        // Указатель head сдвигается на одну позицию назад; если он выходит за конец, то возвращается в начало массива\n        this.#front = (this.#front + 1) % this.capacity;\n        this.#queSize--;\n        return num;\n    }\n\n    /* Доступ к элементу в начале очереди */\n    peek() {\n        if (this.isEmpty()) throw new Error('очередь пуста');\n        return this.#nums[this.#front];\n    }\n\n    /* Вернуть Array */\n    toArray() {\n        // Преобразовывать только элементы списка в пределах фактической длины\n        const arr = new Array(this.size);\n        for (let i = 0, j = this.#front; i < this.size; i++, j++) {\n            arr[i] = this.#nums[j % this.capacity];\n        }\n        return arr;\n    }\n}\n\n/* Driver Code */\n/* Инициализация очереди */\nconst capacity = 10;\nconst queue = new ArrayQueue(capacity);","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/ru/codes/javascript/chapter_stack_and_queue/array_queue.js#L39-L75","documentation":"Thrown by ArrayQueue.peek (JS, array_queue.js) with message 'очередь пуста' ('queue is empty') when reading the head of an empty circular queue. pop() calls peek() first, so the same throw propagates from pop() on an empty queue.","triggerScenarios":"Calling peek() or pop() when #queSize === 0. Common in producer/consumer loops that over-consume.","commonSituations":"BFS exhaustion; worker loop draining a task queue to empty; mismatch between enqueue and dequeue counts.","solutions":["Check isEmpty() (or size === 0) before pop/peek.","In BFS, gate frontier expansion on the queue not being empty.","Capture size once when draining: for (let n = q.size(); n > 0; n--) q.pop()."],"exampleFix":"// before\nwhile (q.size() >= 0) { q.pop(); } // throws on final empty pop\n\n// after\nwhile (!q.isEmpty()) { const v = q.pop(); }","handlingStrategy":"validation","validationCode":"while (!q.isEmpty()) { const v = q.pop(); process(v); }","typeGuard":null,"tryCatchPattern":"try { q.pop(); } catch (e) { if (e.message !== 'очередь пуста') throw e; }","preventionTips":["Use while (!q.isEmpty()) for drain loops, not while (q.size() >= 0).","In BFS, gate frontier expansion on non-empty queue.","Capture size once when draining a known count."],"tags":["queue","javascript","precondition","empty-state","hello-algo"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}