TheAlgorithms/JavaScript · error · TypeError

Input cannot be a Decimal

Error message

Input cannot be a Decimal

What it means

Thrown by aliquotSum(input) as a TypeError when Math.floor(input) !== input, i.e. the value has a fractional part. The aliquot-sum loop indexes divisors by integer steps, so fractional inputs are rejected. Note this check uses Math.floor (not Number.isInteger), so non-number inputs (e.g. strings, NaN) also trip this guard because Math.floor returns NaN and NaN !== NaN.

Source

Thrown at Maths/AliquotSum.js:20

  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. Round to an integer before calling: const n = Math.trunc(input); validate Number.isInteger(n).
  2. Validate at the boundary with Number.isInteger(Number(input)) for a clearer message than the library's.
  3. Reject strings early; coerce with Number() and check finiteness.
  4. Decide a domain rounding policy (floor/round) for fractional inputs before invoking.

Example fix

// before
const s = aliquotSum(value) // throws when value is 6.5 or a non-numeric string

// after
const n = Math.trunc(Number(value))
if (!Number.isInteger(n) || n < 0) throw new TypeError('input must be a non-negative integer')
const s = aliquotSum(n)
Defensive patterns

Strategy: type-guard

Validate before calling

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

Type guard

const isNonNegInt = (v) =>
  typeof v === 'number' && Number.isInteger(v) && v >= 0

Try / catch

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

Prevention

When it happens

Trigger: aliquotSum(6.5); aliquotSum(3.14); aliquotSum('abc') (Math.floor -> NaN, NaN !== 'abc' is true -> throws this message).

Common situations: Float division results fed in without rounding; currency/measurements with decimals; unparsed string inputs that slipped past the negative guard.

Related errors


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