TheAlgorithms/JavaScript · error · TypeError

Not a Number

Error message

Not a Number

What it means

Thrown by extendedEuclideanGCD(arg1, arg2) (ExtendedEuclideanGCD.js:28) as a TypeError when either argument is not of type 'number'. The function computes the GCD along with the Bezout coefficients using an iterative algorithm that assumes numeric operands. A second check immediately after rejects values less than 1 with 'Must be positive numbers', so this specific error fires only on non-number inputs (including BigInt, which is typeof 'bigint').

Source

Thrown at Maths/ExtendedEuclideanGCD.js:29

 * This is called Bézout's identity and the coefficients are called Bézout coefficients
 *
 * The algorithm uses the Euclidean method of getting remainder:
 * r_i+1 = r_i-1 - qi*ri
 * and applies it to series s and t (with same quotient q at each stage)
 * When r_n reaches 0, the value r_n-1 gives the gcd, and s_n-1 and t_n-1 give the coefficients
 *
 * This implementation uses an iterative approach to calculate the values
 */

/**
 *
 * @param {Number} arg1 first argument
 * @param {Number} arg2 second argument
 * @returns Array with GCD and first and second Bézout coefficients
 */
const extendedEuclideanGCD = (arg1, arg2) => {
  if (typeof arg1 !== 'number' || typeof arg2 !== 'number')
    throw new TypeError('Not a Number')
  if (arg1 < 1 || arg2 < 1) throw new TypeError('Must be positive numbers')

  // Make the order of coefficients correct, as the algorithm assumes r0 > r1
  if (arg1 < arg2) {
    const res = extendedEuclideanGCD(arg2, arg1)
    const temp = res[1]
    res[1] = res[2]
    res[2] = temp
    return res
  }

  // At this point arg1 > arg2

  // Remainder values
  let r0 = arg1
  let r1 = arg2

  // Coefficient1 values

View on GitHub (pinned to 5c39e87a9a)

Solutions

  1. Coerce both arguments with Number() and verify Number.isFinite.
  2. If you need large-integer GCD, use a BigInt-native implementation instead.
  3. Catch TypeError at the boundary since this function does throw TypeError.
  4. Validate both args are positive after the type check, since the next guard rejects < 1.

Example fix

// before
const r = extendedEuclideanGCD(a, b) // a or b may be a string

// after
const [x, y] = [a, b].map(Number)
if (![x, y].every(Number.isFinite)) throw new TypeError('expected finite numbers')
const r = extendedEuclideanGCD(x, y)
Defensive patterns

Strategy: type-guard

Validate before calling

const [x, y] = [arg1, arg2].map(Number)
if (![x, y].every(v => Number.isFinite(v) && v >= 1)) {
  throw new TypeError('both arguments must be finite numbers >= 1')
}
const r = extendedEuclideanGCD(x, y)

Type guard

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

Try / catch

try {
  r = extendedEuclideanGCD(a, b)
} catch (e) {
  if (e instanceof TypeError && e.message === 'Not a Number') {
    // non-number input — coerce and retry
  } else throw e
}

Prevention

When it happens

Trigger: Call extendedEuclideanGCD('12', 8) with a string; extendedEuclideanGCD(undefined, 8) from a missing argument; extendedEuclideanGCD(BigInt(12), BigInt(8)); extendedEuclideanGCD(null, null) from a JSON null.

Common situations: Command-line or form inputs parsed as strings; BigInt used to avoid precision loss on large numbers (this function does not support BigInt); optional params that arrived undefined; deserialized JSON with stringified numbers.

Related errors


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