{"record":{"id":"2808ab9d4d67187d","repo":"TheAlgorithms/JavaScript","slug":"the-two-parameters-must-be-distinct-non-null-inte","errorCode":null,"errorMessage":"The two parameters must be distinct, non-null integers","messagePattern":"The two parameters must be distinct, non-null integers","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"Maths/FriendlyNumbers.js","lineNumber":20,"sourceCode":"  'In number theory, friendly numbers are two or more natural numbers with a common abundancy index, the\n  ratio between the sum of divisors of a number and the number itself.'\n  Source: https://en.wikipedia.org/wiki/Friendly_number\n  See also: https://mathworld.wolfram.com/FriendlyNumber.html#:~:text=The%20numbers%20known%20to%20be,numbers%20have%20a%20positive%20density.\n*/\n\nexport const FriendlyNumbers = (firstNumber, secondNumber) => {\n  // input: two integers\n  // output: true if the two integers are friendly numbers, false if they are not friendly numbers\n\n  // First, check that the parameters are valid\n  if (\n    !Number.isInteger(firstNumber) ||\n    !Number.isInteger(secondNumber) ||\n    firstNumber === 0 ||\n    secondNumber === 0 ||\n    firstNumber === secondNumber\n  ) {\n    throw new Error('The two parameters must be distinct, non-null integers')\n  }\n\n  return abundancyIndex(firstNumber) === abundancyIndex(secondNumber)\n}\n\nfunction abundancyIndex(number) {\n  return sumDivisors(number) / number\n}\n\nfunction sumDivisors(number) {\n  let runningSumDivisors = number\n  for (let i = 0; i < number / 2; i++) {\n    if (Number.isInteger(number / i)) {\n      runningSumDivisors += i\n    }\n  }\n  return runningSumDivisors\n}","sourceCodeStart":2,"sourceCodeEnd":38,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Maths/FriendlyNumbers.js#L2-L38","documentation":"Thrown by FriendlyNumbers(firstNumber, secondNumber) (FriendlyNumbers.js:11) as a plain Error when the two arguments fail a combined validity check. The error covers four distinct conditions: either argument is not an integer (Number.isInteger), either argument is 0, or the two arguments are equal. Friendly numbers are pairs of distinct positive integers with the same abundancy index, so all four cases are mathematically out of domain. The single combined message makes it impossible to tell which condition failed from the error alone.","triggerScenarios":"Call FriendlyNumbers(1.5, 3) with a float; FriendlyNumbers(0, 6) with a zero; FriendlyNumbers(6, 6) with equal arguments; FriendlyNumbers('6', 28) with a string (fails Number.isInteger); FriendlyNumbers(NaN, 28).","commonSituations":"Comparing a number against itself due to a copy/paste or aliasing bug; floats introduced by upstream division; defaulting a missing argument to 0; string inputs from a form not coerced to numbers.","solutions":["Pre-validate each argument with Number.isInteger and that it is non-zero, and ensure they are distinct before calling.","If the two values came from the same source, check whether they were intended to be different fields.","Coerce string inputs with Number() and round if near-integer floats are acceptable.","Because the message is non-specific, add your own discriminating checks to give a clearer error to your callers."],"exampleFix":"// before\nconst r = FriendlyNumbers(a, b) // single opaque error\n\n// after\nif (!Number.isInteger(a) || !Number.isInteger(b)) throw new TypeError('both must be integers')\nif (a === 0 || b === 0) throw new RangeError('must be non-zero')\nif (a === b) throw new RangeError('must be distinct')\nconst r = FriendlyNumbers(a, b)","handlingStrategy":"validation","validationCode":"if (\n  !Number.isInteger(firstNumber) || !Number.isInteger(secondNumber) ||\n  firstNumber === 0 || secondNumber === 0 ||\n  firstNumber === secondNumber\n) {\n  throw new Error('inputs must be distinct non-zero integers')\n}\nconst r = FriendlyNumbers(firstNumber, secondNumber)","typeGuard":"const areValidFriendlyArgs = (a, b) =>\n  Number.isInteger(a) && Number.isInteger(b) &&\n  a !== 0 && b !== 0 && a !== b","tryCatchPattern":"try {\n  r = FriendlyNumbers(a, b)\n} catch (e) {\n  if (e instanceof Error && /distinct, non-null integers/.test(e.message)) {\n    // one of four conditions failed — re-validate to give a clearer message\n  } else throw e\n}","preventionTips":["The single error message covers four conditions — add your own discriminating checks for clearer diagnostics.","Ensure the two arguments are genuinely different fields, not aliases of the same value.","Coerce string inputs with Number() and reject floats explicitly before calling."],"tags":["validation","number-theory","integer","input-validation","friendly-numbers"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}