TheAlgorithms/JavaScript · error · Error

The two parameters must be distinct, non-null integers

Error message

The two parameters must be distinct, non-null integers

What it means

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.

Source

Thrown at Maths/FriendlyNumbers.js:20

  'In number theory, friendly numbers are two or more natural numbers with a common abundancy index, the
  ratio between the sum of divisors of a number and the number itself.'
  Source: https://en.wikipedia.org/wiki/Friendly_number
  See also: https://mathworld.wolfram.com/FriendlyNumber.html#:~:text=The%20numbers%20known%20to%20be,numbers%20have%20a%20positive%20density.
*/

export const FriendlyNumbers = (firstNumber, secondNumber) => {
  // input: two integers
  // output: true if the two integers are friendly numbers, false if they are not friendly numbers

  // First, check that the parameters are valid
  if (
    !Number.isInteger(firstNumber) ||
    !Number.isInteger(secondNumber) ||
    firstNumber === 0 ||
    secondNumber === 0 ||
    firstNumber === secondNumber
  ) {
    throw new Error('The two parameters must be distinct, non-null integers')
  }

  return abundancyIndex(firstNumber) === abundancyIndex(secondNumber)
}

function abundancyIndex(number) {
  return sumDivisors(number) / number
}

function sumDivisors(number) {
  let runningSumDivisors = number
  for (let i = 0; i < number / 2; i++) {
    if (Number.isInteger(number / i)) {
      runningSumDivisors += i
    }
  }
  return runningSumDivisors
}

View on GitHub (pinned to 5c39e87a9a)

Solutions

  1. Pre-validate each argument with Number.isInteger and that it is non-zero, and ensure they are distinct before calling.
  2. If the two values came from the same source, check whether they were intended to be different fields.
  3. Coerce string inputs with Number() and round if near-integer floats are acceptable.
  4. Because the message is non-specific, add your own discriminating checks to give a clearer error to your callers.

Example fix

// before
const r = FriendlyNumbers(a, b) // single opaque error

// after
if (!Number.isInteger(a) || !Number.isInteger(b)) throw new TypeError('both must be integers')
if (a === 0 || b === 0) throw new RangeError('must be non-zero')
if (a === b) throw new RangeError('must be distinct')
const r = FriendlyNumbers(a, b)
Defensive patterns

Strategy: validation

Validate before calling

if (
  !Number.isInteger(firstNumber) || !Number.isInteger(secondNumber) ||
  firstNumber === 0 || secondNumber === 0 ||
  firstNumber === secondNumber
) {
  throw new Error('inputs must be distinct non-zero integers')
}
const r = FriendlyNumbers(firstNumber, secondNumber)

Type guard

const areValidFriendlyArgs = (a, b) =>
  Number.isInteger(a) && Number.isInteger(b) &&
  a !== 0 && b !== 0 && a !== b

Try / catch

try {
  r = FriendlyNumbers(a, b)
} catch (e) {
  if (e instanceof Error && /distinct, non-null integers/.test(e.message)) {
    // one of four conditions failed — re-validate to give a clearer message
  } else throw e
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


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