{"record":{"id":"7d1b9919fcf6b846","repo":"TheAlgorithms/JavaScript","slug":"invalid-input-7d1b99","errorCode":null,"errorMessage":"Invalid Input","messagePattern":"Invalid Input","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Recursive/Partition.js","lineNumber":13,"sourceCode":"/**\n * @function canPartition\n * @description Check whether it is possible to partition the given array into two equal sum subsets using recursion.\n * @param {number[]} nums - The input array of numbers.\n * @param {number} index - The current index in the array being considered.\n * @param {number} target - The target sum for each subset.\n * @return {boolean}.\n * @see [Partition Problem](https://en.wikipedia.org/wiki/Partition_problem)\n */\n\nconst canPartition = (nums, index = 0, target = 0) => {\n  if (!Array.isArray(nums)) {\n    throw new TypeError('Invalid Input')\n  }\n\n  const sum = nums.reduce((acc, num) => acc + num, 0)\n\n  if (sum % 2 !== 0) {\n    return false\n  }\n\n  if (target === sum / 2) {\n    return true\n  }\n\n  if (index >= nums.length || target > sum / 2) {\n    return false\n  }\n\n  // Include the current number in the first subset and check if a solution is possible.\n  const withCurrent = canPartition(nums, index + 1, target + nums[index])","sourceCodeStart":1,"sourceCodeEnd":31,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Recursive/Partition.js#L1-L31","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure the first argument is an actual Array via Array.isArray(nums) before calling.","If the source may be absent, default it: canPartition(nums ?? []).","Validate element types too, since the guard does not: nums.every(n => typeof n === 'number')."],"exampleFix":"// before\nconst ok = canPartition(maybeArray)\n\n// after\nif (Array.isArray(nums) && nums.every(n => typeof n === 'number')) {\n  const ok = canPartition(nums)\n}","handlingStrategy":"validation","validationCode":"function safeCanPartition(nums) {\n  if (!Array.isArray(nums) || !nums.every(n => typeof n === 'number' && Number.isFinite(n))) {\n    throw new TypeError('Expected an array of finite numbers')\n  }\n  return canPartition(nums)\n}","typeGuard":"function isNumberArray(v) {\n  return Array.isArray(v) && v.every(n => typeof n === 'number' && Number.isFinite(n))\n}","tryCatchPattern":"try {\n  canPartition(nums)\n} catch (e) {\n  if (e instanceof TypeError && e.message === 'Invalid Input') {\n    return false\n  }\n  throw e\n}","preventionTips":["Validate the array and its elements at the data source.","Default optional array parameters with ?? [].","Filter out non-numeric entries before partitioning."],"tags":["type-check","array","dynamic-programming","input-validation"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}