krahets/hello-algo · error · Error
индекс выходит за границы
Error message
индекс выходит за границы
What it means
Thrown by get(index) on the custom MyList when `index < 0 || index >= this.#size`. Plain Error with a Russian message ('index out of bounds'). It guards read access against the logical size (#size), not the backing array capacity (#capacity).
Source
Thrown at ru/codes/javascript/chapter_array_and_linkedlist/my_list.js:32
/* Конструктор */
constructor() {
this.#arr = new Array(this.#capacity);
}
/* Получить длину списка (текущее число элементов) */
size() {
return this.#size;
}
/* Получить вместимость списка */
capacity() {
return this.#capacity;
}
/* Доступ к элементу */
get(index) {
// Если индекс выходит за границы, выбрасывается исключение; далее аналогично
if (index < 0 || index >= this.#size) throw new Error('индекс выходит за границы');
return this.#arr[index];
}
/* Обновление элемента */
set(index, num) {
if (index < 0 || index >= this.#size) throw new Error('индекс выходит за границы');
this.#arr[index] = num;
}
/* Добавление элемента в конец */
add(num) {
// Если длина равна вместимости, требуется расширение
if (this.#size === this.#capacity) {
this.extendCapacity();
}
// Добавить новый элемент в конец списка
this.#arr[this.#size] = num;
this.#size++;View on GitHub (pinned to 69932aed18)
Solutions
- Bounds-check `index >= 0 && index < list.size()` before get.
- Use `for (i=0; i<list.size(); i++)` (strict less-than).
- Validate external/deserialized indices before access.
Example fix
// before const v = list.get(idx); // after const v = (idx >= 0 && idx < list.size()) ? list.get(idx) : undefined;
Defensive patterns
Strategy: validation
Validate before calling
// Bounds-check against logical size before get.
if (index >= 0 && index < list.size()) {
const v = list.get(index);
} Type guard
function isValidListIndex(list, index) {
return Number.isInteger(index) && index >= 0 && index < list.size();
} Try / catch
try {
const v = list.get(index);
} catch (e) {
if (e instanceof Error && e.message === 'индекс выходит за границы') {
// out-of-range access
} else throw e;
} Prevention
- Loop with strict `i < list.size()`, never `i <= size`.
- Validate user-supplied or deserialized indices before access.
- Remember bounds are against size, not capacity.
- Recompute indices after remove/insert.
When it happens
Trigger: Calling get(-1); calling get(size) (one past the last valid index); calling get on a freshly constructed list with no added elements; using an index from a deserialized list whose size was not restored.
Common situations: Off-by-one loops `for (i=0; i<=size; i++)`; confusing size with capacity; ignoring the return of remove/shrink and reusing a stale index; user-supplied index not validated.
Related errors
AI-assisted analysis of krahets/hello-algo@69932aed18 (2026-08-13).
Data as JSON: /api/errors/2585f9f1388326a1.
Report an issue: GitHub.