TheAlgorithms/JavaScript · error · TypeError

Invalid Input

Error message

Invalid Input

What it means

Thrown as a TypeError by canPartition() when the first argument is not an array. The function computes the sum via nums.reduce() and indexes into nums, so a non-array would either produce a TypeError deeper in reduce or yield incorrect results. Note the check only validates that nums is an array; it does not verify elements are numbers, and index/target have defaults.

Source

Thrown at Recursive/Partition.js:13

/**
 * @function canPartition
 * @description Check whether it is possible to partition the given array into two equal sum subsets using recursion.
 * @param {number[]} nums - The input array of numbers.
 * @param {number} index - The current index in the array being considered.
 * @param {number} target - The target sum for each subset.
 * @return {boolean}.
 * @see [Partition Problem](https://en.wikipedia.org/wiki/Partition_problem)
 */

const canPartition = (nums, index = 0, target = 0) => {
  if (!Array.isArray(nums)) {
    throw new TypeError('Invalid Input')
  }

  const sum = nums.reduce((acc, num) => acc + num, 0)

  if (sum % 2 !== 0) {
    return false
  }

  if (target === sum / 2) {
    return true
  }

  if (index >= nums.length || target > sum / 2) {
    return false
  }

  // Include the current number in the first subset and check if a solution is possible.
  const withCurrent = canPartition(nums, index + 1, target + nums[index])

View on GitHub (pinned to 5c39e87a9a)

Solutions

  1. Ensure the first argument is an actual Array via Array.isArray(nums) before calling.
  2. If the source may be absent, default it: canPartition(nums ?? []).
  3. Validate element types too, since the guard does not: nums.every(n => typeof n === 'number').

Example fix

// before
const ok = canPartition(maybeArray)

// after
if (Array.isArray(nums) && nums.every(n => typeof n === 'number')) {
  const ok = canPartition(nums)
}
Defensive patterns

Strategy: validation

Validate before calling

function safeCanPartition(nums) {
  if (!Array.isArray(nums) || !nums.every(n => typeof n === 'number' && Number.isFinite(n))) {
    throw new TypeError('Expected an array of finite numbers')
  }
  return canPartition(nums)
}

Type guard

function isNumberArray(v) {
  return Array.isArray(v) && v.every(n => typeof n === 'number' && Number.isFinite(n))
}

Try / catch

try {
  canPartition(nums)
} catch (e) {
  if (e instanceof TypeError && e.message === 'Invalid Input') {
    return false
  }
  throw e
}

Prevention

When it happens

Trigger: Calling canPartition(null), canPartition(undefined), canPartition('1234'), canPartition(1234), or canPartition({0:1,1:2}). The reduce on a non-array is the most common native failure that this guard preempts.

Common situations: Deserializing JSON where the expected array field is missing or null; passing a single number instead of a list; a pipeline stage that filters the array down to undefined.

Related errors


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