TheAlgorithms/JavaScript · error · Error
Index out of range
Error message
Index out of range
What it means
Thrown by findMaxRecursion(arr, left, right) (FindMaxRecursion.js:24) as a plain Error when left or right falls outside the valid index range of the array. The function accepts negative indices (down to -len) as well as positive indices, mirroring JS array indexing, and rejects anything beyond those bounds. Note: an empty array does NOT throw here — it returns undefined at line 20, so the bounds check only matters for non-empty arrays.
Source
Thrown at Maths/FindMaxRecursion.js:25
* @param {Integer} right Index of the last element
*
* @return {Integer} Maximum value of the array
*
* @see [Maximum value](https://en.wikipedia.org/wiki/Maximum_value)
*
* @example findMaxRecursion([1, 2, 4, 5]) = 5
* @example findMaxRecursion([10, 40, 100, 20]) = 100
* @example findMaxRecursion([-1, -2, -4, -5]) = -1
*/
function findMaxRecursion(arr, left, right) {
const len = arr.length
if (len === 0 || !arr) {
return undefined
}
if (left >= len || left < -len || right >= len || right < -len) {
throw new Error('Index out of range')
}
if (left === right) {
return arr[left]
}
// n >> m is equivalent to floor(n / pow(2, m)), floor(n / 2) in this case, which is the mid index
const mid = (left + right) >> 1
const leftMax = findMaxRecursion(arr, left, mid)
const rightMax = findMaxRecursion(arr, mid + 1, right)
// Return the maximum
return Math.max(leftMax, rightMax)
}
export { findMaxRecursion }
View on GitHub (pinned to 5c39e87a9a)
Solutions
- Use the conventional call pattern: findMaxRecursion(arr, 0, arr.length - 1).
- Clamp indices before calling: left = Math.max(0, left); right = Math.min(arr.length - 1, right).
- Always pass both left and right explicitly — the function has no defaults.
- Recompute indices from the current array length rather than caching them.
Example fix
// before const m = findMaxRecursion(arr, 0, arr.length) // off-by-one: length not length-1 // after const m = findMaxRecursion(arr, 0, arr.length - 1)
Defensive patterns
Strategy: validation
Validate before calling
if (!Array.isArray(arr) || arr.length === 0) return undefined const left = Math.max(0, Math.min(start, arr.length - 1)) const right = Math.max(0, Math.min(end, arr.length - 1)) const m = findMaxRecursion(arr, left, right)
Type guard
const areValidIndices = (arr, l, r) => Array.isArray(arr) && arr.length > 0 && l >= -arr.length && l < arr.length && r >= -arr.length && r < arr.length
Try / catch
try {
m = findMaxRecursion(arr, left, right)
} catch (e) {
if (e instanceof Error && e.message === 'Index out of range') {
// clamp indices to bounds and retry
m = findMaxRecursion(arr, 0, arr.length - 1)
} else throw e
} Prevention
- Use the canonical call findMaxRecursion(arr, 0, arr.length - 1) — never arr.length.
- Always pass both left and right; there are no defaults.
- Recompute indices from current array length rather than caching them.
When it happens
Trigger: Call findMaxRecursion([1,2,3], 0, 5) where right=5 exceeds length 3; findMaxRecursion([1,2,3], -4, 2) where left=-4 is below -len (-3); passing left/right that were computed from a different (longer or shorter) array than the one supplied; omitting right so it becomes undefined and fails the comparison.
Common situations: Off-by-one errors computing the last index (using arr.length instead of arr.length - 1); stale indices cached before the array was mutated/truncated; omitting the right argument expecting a default (there is no default); passing indices from a 0-based vs 1-based source mismatch.
Related errors
- Index Out of Bound
- Input is not a valid 2D matrix.
- Array is empty
- Invalid Month Number.
- Provided input is not an array
AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13).
Data as JSON: /api/errors/ad44d0bdbfcf9fbb.
Report an issue: GitHub.