{"record":{"id":"a564838613ef13c4","repo":"TheAlgorithms/JavaScript","slug":"given-input-is-not-an-array","errorCode":null,"errorMessage":"Given input is not an array","messagePattern":"Given input is not an array","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Sorts/SelectionSort.js","lineNumber":13,"sourceCode":"/* The selection sort algorithm sorts an array by repeatedly finding the minimum element\n *(considering ascending order) from unsorted part and putting it at the beginning. The\n *algorithm maintains two subarrays in a given array.\n *1) The subarray which is already sorted.\n *2) Remaining subarray which is unsorted.\n *\n *In every iteration of selection sort, the minimum element (considering ascending order)\n *from the unsorted subarray is picked and moved to the sorted subarray.\n */\n\nexport const selectionSort = (list) => {\n  if (!Array.isArray(list)) {\n    throw new TypeError('Given input is not an array')\n  }\n  const items = [...list] // We don't want to modify the original array\n  const length = items.length\n  for (let i = 0; i < length - 1; i++) {\n    if (typeof items[i] !== 'number') {\n      throw new TypeError('One of the items in your array is not a number')\n    }\n    // Number of passes\n    let min = i // min holds the current minimum number position for each pass; i holds the Initial min number\n    for (let j = i + 1; j < length; j++) {\n      // Note that j = i + 1 as we only need to go through unsorted array\n      if (items[j] < items[min]) {\n        // Compare the numbers\n        min = j // Change the current min number position if a smaller num is found\n      }\n    }\n    if (min !== i) {\n      // After each pass, if the current min num != initial min num, exchange the position.","sourceCodeStart":1,"sourceCodeEnd":31,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Sorts/SelectionSort.js#L1-L31","documentation":"Thrown as a TypeError by selectionSort() when the input list is not an array. The function spreads the input ([...list]) and indexes it, so a non-array would either spread incorrectly or fail. This is the outer type guard; a separate inner check (error 130) validates element types.","triggerScenarios":"Calling selectionSort(null), selectionSort(undefined), selectionSort('dcba'), selectionSort(4321), or selectionSort({0:3}). Passing a string is common because strings are array-like but not arrays.","commonSituations":"User input from a text field passed unmodified; a value that is sometimes null from a lookup; treating a NodeList as an array without conversion.","solutions":["Convert array-like objects: selectionSort(Array.from(nodeList)).","Guard at the boundary: if (!Array.isArray(x)) throw new TypeError('expected array').","Coerce strings if numeric sorting is intended: selectionSort([...str].map(Number))."],"exampleFix":"// before\nconst sorted = selectionSort(maybeString)\n\n// after\nconst arr = Array.isArray(maybeString) ? maybeString : Array.from(maybeString)\nconst sorted = selectionSort(arr)","handlingStrategy":"type-guard","validationCode":"function safeSelectionSort(list) {\n  if (!Array.isArray(list)) {\n    throw new TypeError('Expected an array')\n  }\n  return selectionSort(list)\n}","typeGuard":"function isArray(v) {\n  return Array.isArray(v)\n}","tryCatchPattern":"try {\n  selectionSort(list)\n} catch (e) {\n  if (e instanceof TypeError && e.message === 'Given input is not an array') {\n    return selectionSort(Array.from(list))\n  }\n  throw e\n}","preventionTips":["Convert strings/NodeLists/Sets to arrays with Array.from before sorting.","Guard at boundaries where data type is uncertain.","Avoid passing array-like objects directly."],"tags":["type-check","sort","input-validation"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}