{"record":{"id":"10951f4b7ab260c8","repo":"TheAlgorithms/JavaScript","slug":"invalid-input-10951f","errorCode":null,"errorMessage":"Invalid Input","messagePattern":"Invalid Input","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"Project-Euler/Problem021.js","lineNumber":19,"sourceCode":"import { aliquotSum } from '../Maths/AliquotSum.js'\n\n/**\n * Problem 21 - Amicable numbers\n *\n * @see {@link https://projecteuler.net/problem=21}\n *\n * Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n).\n * If d(a) = b and d(b) = a, where a != b, then a and b are an amicable pair and each of a and b are called amicable numbers.\n * For example, the proper divisors of 220 are 1,2,4,5,10,11,20,22,44,55 and 110; therefore d(220) = 284.\n * The proper divisors of 284 are 1,2,4,71 and 142; so d(284) = 220.\n * Evaluate the sum of all amicable numbers under 10000\n *\n * @author PraneethJain\n */\n\nfunction problem21(n) {\n  if (n < 2) {\n    throw new Error('Invalid Input')\n  }\n\n  let result = 0\n  for (let a = 2; a < n; ++a) {\n    const b = aliquotSum(a) // Sum of all proper divisors of a\n    // Check if b > a to ensure each pair isn't counted twice, and check if sum of proper divisors of b is equal to a\n    if (b > a && aliquotSum(b) === a) {\n      result += a + b\n    }\n  }\n  return result\n}\n\nexport { problem21 }\n","sourceCodeStart":1,"sourceCodeEnd":34,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Project-Euler/Problem021.js#L1-L34","documentation":"Thrown by `problem21(n)` (Project Euler #21, amicable numbers) when `n < 2`. The search loop starts at `a = 2`, so an upper bound below 2 yields nothing. The message is generic. Non-number inputs bypass the guard because comparisons with undefined/NaN return false.","triggerScenarios":"Call `problem21(0)`, `problem21(1)`, `problem21(-5)`. `n = 2` passes but finds no amicable pairs below 2.","commonSituations":"Defaulting an 'upper limit' field to 0 or 1, off-by-one from inclusive vs exclusive interpretation, or a request param parsed as 1.","solutions":["Validate `n >= 2` and is a finite integer upstream.","Coerce with `Number(n)` so non-numbers do not slip past the guard.","Pick a sensible default (e.g. 10000) when the field is missing."],"exampleFix":"// before\nproblem21(upperBound) // upperBound could be 1\n\n// after\nconst n = Number(upperBound)\nif (!Number.isInteger(n) || n < 2) throw new RangeError('n must be an integer >= 2')\nproblem21(n)","handlingStrategy":"validation","validationCode":"const n = Number(bound)\nif (!Number.isInteger(n) || n < 2) {\n  throw new RangeError('n must be an integer >= 2')\n}\nproblem21(n)","typeGuard":"const isIntAtLeast2 = (x) => typeof x === 'number' && Number.isInteger(x) && x >= 2","tryCatchPattern":null,"preventionTips":["Coerce with Number() so undefined/NaN do not bypass the guard.","Pick a sensible default (e.g. 10000) when the bound is missing.","Distinguish 'inclusive' vs 'exclusive' upper bound at the boundary."],"tags":["range-validation","project-euler","amicable-numbers","generic-message"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}