{"record":{"id":"5979d0cc00e40e1c","repo":"TheAlgorithms/JavaScript","slug":"one-of-the-items-in-your-array-is-not-a-number","errorCode":null,"errorMessage":"One of the items in your array is not a number","messagePattern":"One of the items in your array is not a number","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Sorts/SelectionSort.js","lineNumber":19,"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.\n      // Swap the numbers\n      ;[items[i], items[min]] = [items[min], items[i]]\n    }\n  }\n  return items\n}","sourceCodeStart":1,"sourceCodeEnd":37,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Sorts/SelectionSort.js#L1-L37","documentation":"Thrown as a TypeError by selectionSort() when an element of the input array fails typeof items[i] !== 'number'. Selection sort compares elements with <, so non-numeric elements would produce inconsistent ordering. Note the check runs only in the outer loop over i, so the inner-loop comparison values (items[j]) are not independently type-checked, and the last element is never validated by this loop (it stops at length-1).","triggerScenarios":"Calling selectionSort([3, '1', 2]), selectionSort([1, null, 3]), selectionSort([1, undefined]), selectionSort([5, {x:1}]), or selectionSort([1, NaN]) — note NaN passes typeof but breaks sorting silently.","commonSituations":"Mixed-type arrays from loose JSON; null/undefined holes from sparse data; values coerced from query strings that include empty strings.","solutions":["Sanitize the array before sorting: arr.filter(x => typeof x === 'number' && !Number.isNaN(x)).","Coerce elements: arr.map(Number).filter(n => !Number.isNaN(n)).","Validate at the data source so the array is always numeric."],"exampleFix":"// before\nconst sorted = selectionSort(rawData)\n\n// after\nconst clean = rawData.filter(x => typeof x === 'number' && !Number.isNaN(x))\nconst sorted = selectionSort(clean)","handlingStrategy":"validation","validationCode":"function safeSelectionSort(list) {\n  if (!Array.isArray(list)) throw new TypeError('Expected an array')\n  const clean = list.filter(x => typeof x === 'number' && !Number.isNaN(x))\n  return selectionSort(clean)\n}","typeGuard":"function isAllNumbers(arr) {\n  return Array.isArray(arr) && arr.every(x => typeof x === 'number' && !Number.isNaN(x))\n}","tryCatchPattern":"try {\n  selectionSort(list)\n} catch (e) {\n  if (e instanceof TypeError && e.message.includes('not a number')) {\n    return selectionSort(list.filter(x => typeof x === 'number' && !Number.isNaN(x)))\n  }\n  throw e\n}","preventionTips":["Filter non-numeric and NaN values before sorting.","Coerce with .map(Number) and drop NaNs.","Validate element types at the data source."],"tags":["type-check","sort","numeric","input-validation"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}