{"record":{"id":"ad44d0bdbfcf9fbb","repo":"TheAlgorithms/JavaScript","slug":"index-out-of-range","errorCode":null,"errorMessage":"Index out of range","messagePattern":"Index out of range","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"Maths/FindMaxRecursion.js","lineNumber":25,"sourceCode":" * @param {Integer} right Index of the last element\n *\n * @return {Integer} Maximum value of the array\n *\n * @see [Maximum value](https://en.wikipedia.org/wiki/Maximum_value)\n *\n * @example findMaxRecursion([1, 2, 4, 5]) = 5\n * @example findMaxRecursion([10, 40, 100, 20]) = 100\n * @example findMaxRecursion([-1, -2, -4, -5]) = -1\n */\nfunction findMaxRecursion(arr, left, right) {\n  const len = arr.length\n\n  if (len === 0 || !arr) {\n    return undefined\n  }\n\n  if (left >= len || left < -len || right >= len || right < -len) {\n    throw new Error('Index out of range')\n  }\n\n  if (left === right) {\n    return arr[left]\n  }\n\n  // n >> m is equivalent to floor(n / pow(2, m)), floor(n / 2) in this case, which is the mid index\n  const mid = (left + right) >> 1\n\n  const leftMax = findMaxRecursion(arr, left, mid)\n  const rightMax = findMaxRecursion(arr, mid + 1, right)\n\n  // Return the maximum\n  return Math.max(leftMax, rightMax)\n}\n\nexport { findMaxRecursion }\n","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Maths/FindMaxRecursion.js#L7-L43","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nconst m = findMaxRecursion(arr, 0, arr.length) // off-by-one: length not length-1\n\n// after\nconst m = findMaxRecursion(arr, 0, arr.length - 1)","handlingStrategy":"validation","validationCode":"if (!Array.isArray(arr) || arr.length === 0) return undefined\nconst left = Math.max(0, Math.min(start, arr.length - 1))\nconst right = Math.max(0, Math.min(end, arr.length - 1))\nconst m = findMaxRecursion(arr, left, right)","typeGuard":"const areValidIndices = (arr, l, r) =>\n  Array.isArray(arr) && arr.length > 0 &&\n  l >= -arr.length && l < arr.length &&\n  r >= -arr.length && r < arr.length","tryCatchPattern":"try {\n  m = findMaxRecursion(arr, left, right)\n} catch (e) {\n  if (e instanceof Error && e.message === 'Index out of range') {\n    // clamp indices to bounds and retry\n    m = findMaxRecursion(arr, 0, arr.length - 1)\n  } else throw e\n}","preventionTips":["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."],"tags":["validation","array","index-out-of-bounds","recursion","off-by-one"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}