TheAlgorithms/JavaScript · error · Error

Type of n must be number

Error message

Type of n must be number

What it means

Thrown by isAutomorphic(n) (AutomorphicNumber.js:19) as a plain Error when n is not of type 'number'. An automorphic number is one whose square ends in the number itself (e.g. 25 -> 625), and the digit-extraction algorithm relies on arithmetic operators that behave incorrectly on non-numbers. Note the library throws plain Error rather than TypeError despite this being a type check, and note the doc-comment convention: negatives return false, but non-numbers and floats throw.

Source

Thrown at Maths/AutomorphicNumber.js:20

 * @function isAutomorphic
 * @author [SilverDragonOfR] (https://github.com/SilverDragonOfR)
 *
 * @see [Automorphic] (https://en.wikipedia.org/wiki/Automorphic_number)
 * @description This script will check whether a number is Automorphic or not
 * @description A number n is said to be a Automorphic number if the square of n ends in the same digits as n itself.
 *
 * @param {Integer} n - the n for nth Catalan Number
 * @return {Integer} - the nth Catalan Number
 * @complexity Time: O(log10(n)) , Space: O(1)
 *
 * @convention We define Automorphic only for whole number integers. For negetive integer we return False. For float or String we show error.
 * @examples 0, 1, 5, 6, 25, 76, 376, 625, 9376 are some Automorphic numbers
 */

// n is the number to be checked
export const isAutomorphic = (n) => {
  if (typeof n !== 'number') {
    throw new Error('Type of n must be number')
  }
  if (!Number.isInteger(n)) {
    throw new Error('n cannot be a floating point number')
  }
  if (n < 0) {
    return false
  }

  // now n is a whole number integer >= 0
  let n_sq = n * n
  while (n > 0) {
    if (n % 10 !== n_sq % 10) {
      return false
    }
    n = Math.floor(n / 10)
    n_sq = Math.floor(n_sq / 10)
  }

View on GitHub (pinned to 5c39e87a9a)

Solutions

  1. Convert the input with Number() and verify !Number.isNaN(n) before calling.
  2. If you use BigInt for large automorphic checks, note this function does not support it; convert with Number() only if value is within safe integer range.
  3. Wrap the call and catch plain Error (the library does not throw TypeError here).
  4. Validate at the API boundary with a type guard before passing through.

Example fix

// before
const result = isAutomorphic(userInput) // userInput may be a string

// after
const n = Number(userInput)
if (!Number.isFinite(n)) throw new TypeError('expected a finite number')
const result = isAutomorphic(n)
Defensive patterns

Strategy: type-guard

Validate before calling

const n = Number(input)
if (!Number.isFinite(n)) {
  throw new TypeError('expected a finite number')
}
const result = isAutomorphic(n)

Type guard

const isFiniteNumber = (v) => typeof v === 'number' && Number.isFinite(v)

Try / catch

try {
  result = isAutomorphic(n)
} catch (e) {
  if (e instanceof Error && e.message === 'Type of n must be number') {
    // non-number input — coerce and retry with Number()
  } else throw e
}

Prevention

When it happens

Trigger: Call isAutomorphic('25') with a string; isAutomorphic(null) or isAutomorphic(undefined) from an unguarded optional field; isAutomorphic(BigInt(25)) which is typeof 'bigint' not 'number'.

Common situations: Parsing input with parseInt/Number but forgetting to handle NaN; values pulled from JSON where the field was a string; BigInt usage introduced for large numbers (BigInt is not typeof 'number').

Related errors


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