TheAlgorithms/JavaScript · error · TypeError

Input cannot be Negative

Error message

Input cannot be Negative

What it means

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.

Source

Thrown at Maths/AliquotSum.js:16

/*
  A program to calculate the Aliquot Sum of a number.
  The aliquot sum of a number n, is the sum of all the proper divisors of n apart from n itself
  For example, for the number 6
  The divisors are 1, 2, 3 (we don't consider 6), so its aliquot sum is 1 + 2 + 3 = 6
  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)
  For all prime numbers, the aliquot sum is 1, since their only divisor apart from themselves is 1
  Article on Aliquot Sum: https://en.wikipedia.org/wiki/Aliquot_sum
 */

/**
 * @param {Number} input The number whose aliquot sum you want to calculate
 */
function aliquotSum(input) {
  // input can't be negative
  if (input < 0) throw new TypeError('Input cannot be Negative')

  // input can't be a decimal
  if (Math.floor(input) !== input)
    throw new TypeError('Input cannot be a Decimal')

  // Dealing with 1, which isn't a prime
  if (input === 1) return 0

  let sum = 0
  for (let i = 1; i <= input / 2; i++) {
    if (input % i === 0) sum += i
  }

  return sum
}

export { aliquotSum }

View on GitHub (pinned to 5c39e87a9a)

Solutions

  1. Validate input >= 0 before calling aliquotSum.
  2. If negatives can occur legitimately, decide on a domain policy (reject, treat as 0, or abs) at the boundary.
  3. Avoid -1 sentinels for 'unset'; use null and branch.
  4. Recompute inputs from fresh sources after operations that may produce negatives.

Example fix

// before
const s = aliquotSum(n) // throws when n < 0

// after
if (n < 0) throw new RangeError('input must be non-negative')
const s = aliquotSum(n)
Defensive patterns

Strategy: validation

Validate before calling

function safeAliquotSum(input) {
  const n = Number(input)
  if (!Number.isFinite(n) || n < 0) {
    throw new RangeError('input must be a non-negative integer')
  }
  return aliquotSum(n)
}

Type guard

const isNonNegative = (n) => typeof n === 'number' && n >= 0

Try / catch

try {
  return aliquotSum(input)
} catch (e) {
  if (e instanceof TypeError && /negative/i.test(e.message)) {
    return aliquotSum(Math.abs(input))
  }
  throw e
}

Prevention

When it happens

Trigger: aliquotSum(-5); aliquotSum(-0.5); a target computed from subtraction that went negative.

Common situations: Signed inputs from subtraction; user-supplied negatives; reused variable initialized to -1.

Related errors


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