{"record":{"id":"e040a92c7c9d9593","repo":"TheAlgorithms/JavaScript","slug":"input-cannot-be-negative","errorCode":null,"errorMessage":"Input cannot be Negative","messagePattern":"Input cannot be Negative","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Maths/AliquotSum.js","lineNumber":16,"sourceCode":"/*\n  A program to calculate the Aliquot Sum of a number.\n  The aliquot sum of a number n, is the sum of all the proper divisors of n apart from n itself\n  For example, for the number 6\n  The divisors are 1, 2, 3 (we don't consider 6), so its aliquot sum is 1 + 2 + 3 = 6\n  1 is the only number whose aliquot sum is 0 (since its only divisor is 1 and aliquot sum of a number couldn't have itself)\n  For all prime numbers, the aliquot sum is 1, since their only divisor apart from themselves is 1\n  Article on Aliquot Sum: https://en.wikipedia.org/wiki/Aliquot_sum\n */\n\n/**\n * @param {Number} input The number whose aliquot sum you want to calculate\n */\nfunction aliquotSum(input) {\n  // input can't be negative\n  if (input < 0) throw new TypeError('Input cannot be Negative')\n\n  // input can't be a decimal\n  if (Math.floor(input) !== input)\n    throw new TypeError('Input cannot be a Decimal')\n\n  // Dealing with 1, which isn't a prime\n  if (input === 1) return 0\n\n  let sum = 0\n  for (let i = 1; i <= input / 2; i++) {\n    if (input % i === 0) sum += i\n  }\n\n  return sum\n}\n\nexport { aliquotSum }\n","sourceCodeStart":1,"sourceCodeEnd":34,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Maths/AliquotSum.js#L1-L34","documentation":"Thrown by aliquotSum(input) as a TypeError when input < 0. The aliquot sum is defined for non-negative integers (its loop runs from 1 to input/2), so a negative bound is rejected up front. Note this guard runs before the decimal check, and before any type verification, so non-number inputs that are not less than 0 fall through to the decimal guard.","triggerScenarios":"aliquotSum(-5); aliquotSum(-0.5); a target computed from subtraction that went negative.","commonSituations":"Signed inputs from subtraction; user-supplied negatives; reused variable initialized to -1.","solutions":["Validate input >= 0 before calling aliquotSum.","If negatives can occur legitimately, decide on a domain policy (reject, treat as 0, or abs) at the boundary.","Avoid -1 sentinels for 'unset'; use null and branch.","Recompute inputs from fresh sources after operations that may produce negatives."],"exampleFix":"// before\nconst s = aliquotSum(n) // throws when n < 0\n\n// after\nif (n < 0) throw new RangeError('input must be non-negative')\nconst s = aliquotSum(n)","handlingStrategy":"validation","validationCode":"function safeAliquotSum(input) {\n  const n = Number(input)\n  if (!Number.isFinite(n) || n < 0) {\n    throw new RangeError('input must be a non-negative integer')\n  }\n  return aliquotSum(n)\n}","typeGuard":"const isNonNegative = (n) => typeof n === 'number' && n >= 0","tryCatchPattern":"try {\n  return aliquotSum(input)\n} catch (e) {\n  if (e instanceof TypeError && /negative/i.test(e.message)) {\n    return aliquotSum(Math.abs(input))\n  }\n  throw e\n}","preventionTips":["Validate input >= 0 before calling.","Don't use -1 as an 'unset' sentinel; use null and branch.","Decide a domain policy for negatives (reject, abs, or 0) at the boundary.","Recompute inputs from fresh sources after subtraction operations."],"tags":["math","aliquot-sum","type-error","negative-input"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}