TheAlgorithms/JavaScript · error · TypeError

One of the items in your array is not a number

Error message

One of the items in your array is not a number

What it means

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).

Source

Thrown at Sorts/SelectionSort.js:19

/* The selection sort algorithm sorts an array by repeatedly finding the minimum element
 *(considering ascending order) from unsorted part and putting it at the beginning. The
 *algorithm maintains two subarrays in a given array.
 *1) The subarray which is already sorted.
 *2) Remaining subarray which is unsorted.
 *
 *In every iteration of selection sort, the minimum element (considering ascending order)
 *from the unsorted subarray is picked and moved to the sorted subarray.
 */

export const selectionSort = (list) => {
  if (!Array.isArray(list)) {
    throw new TypeError('Given input is not an array')
  }
  const items = [...list] // We don't want to modify the original array
  const length = items.length
  for (let i = 0; i < length - 1; i++) {
    if (typeof items[i] !== 'number') {
      throw new TypeError('One of the items in your array is not a number')
    }
    // Number of passes
    let min = i // min holds the current minimum number position for each pass; i holds the Initial min number
    for (let j = i + 1; j < length; j++) {
      // Note that j = i + 1 as we only need to go through unsorted array
      if (items[j] < items[min]) {
        // Compare the numbers
        min = j // Change the current min number position if a smaller num is found
      }
    }
    if (min !== i) {
      // After each pass, if the current min num != initial min num, exchange the position.
      // Swap the numbers
      ;[items[i], items[min]] = [items[min], items[i]]
    }
  }
  return items
}

View on GitHub (pinned to 5c39e87a9a)

Solutions

  1. Sanitize the array before sorting: arr.filter(x => typeof x === 'number' && !Number.isNaN(x)).
  2. Coerce elements: arr.map(Number).filter(n => !Number.isNaN(n)).
  3. Validate at the data source so the array is always numeric.

Example fix

// before
const sorted = selectionSort(rawData)

// after
const clean = rawData.filter(x => typeof x === 'number' && !Number.isNaN(x))
const sorted = selectionSort(clean)
Defensive patterns

Strategy: validation

Validate before calling

function safeSelectionSort(list) {
  if (!Array.isArray(list)) throw new TypeError('Expected an array')
  const clean = list.filter(x => typeof x === 'number' && !Number.isNaN(x))
  return selectionSort(clean)
}

Type guard

function isAllNumbers(arr) {
  return Array.isArray(arr) && arr.every(x => typeof x === 'number' && !Number.isNaN(x))
}

Try / catch

try {
  selectionSort(list)
} catch (e) {
  if (e instanceof TypeError && e.message.includes('not a number')) {
    return selectionSort(list.filter(x => typeof x === 'number' && !Number.isNaN(x)))
  }
  throw e
}

Prevention

When it happens

Trigger: 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.

Common situations: Mixed-type arrays from loose JSON; null/undefined holes from sparse data; values coerced from query strings that include empty strings.

Related errors


AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13). Data as JSON: /api/errors/5979d0cc00e40e1c. Report an issue: GitHub.