TheAlgorithms/JavaScript · error · TypeError

Argument is not a number.

Error message

Argument is not a number.

What it means

Thrown by CheckKishnamurthyNumber(number) (CheckKishnamurthyNumber.js:26) as a TypeError when the argument is not of type 'number'. A Krishnamurthy (strong) number equals the sum of the factorials of its digits (e.g. 145 = 1!+4!+5!); the digit-extraction loop uses modulo and comparison operators that misbehave on non-numbers. The function special-cases number === 0 returning false before entering the loop.

Source

Thrown at Maths/CheckKishnamurthyNumber.js:27

const factorial = (n) => {
  let fact = 1
  while (n !== 0) {
    fact = fact * n
    n--
  }
  return fact
}

/**
 * krishnamurthy number is a number the sum of the factorial of the all dights is equal to the number itself.
 * @param {Number} number a number for checking is krishnamurthy number or not.
 * @returns return correspond boolean value, if the number is krishnamurthy number return `true` else return `false`.
 * @example 145 => 1! + 4! + 5! = 1  + 24 + 120 = 145
 */
const CheckKishnamurthyNumber = (number) => {
  // firstly, check that input is a number or not.
  if (typeof number !== 'number') {
    throw new TypeError('Argument is not a number.')
  }
  if (number === 0) {
    return false
  }
  // create a variable to store the sum of all digits factorial.
  let sumOfAllDigitFactorial = 0
  // convert the number to string for convenience.
  let newNumber = number
  // Extract number digits using the remainder method.
  while (newNumber > 0) {
    const lastDigit = newNumber % 10
    // calculate each digit factorial.
    sumOfAllDigitFactorial += factorial(lastDigit)
    newNumber = Math.floor(newNumber / 10)
  }
  // if the sumOfAllDigitFactorial is equal to the given number it means the number is a Krishnamurthy number.
  return sumOfAllDigitFactorial === number
}

View on GitHub (pinned to 5c39e87a9a)

Solutions

  1. Coerce with Number() and verify with Number.isFinite before calling.
  2. Add a type guard at the call boundary.
  3. Catch TypeError specifically since this function does throw TypeError (unlike some sibling modules).
  4. For string inputs, also strip whitespace and validate format before Number().

Example fix

// before
const r = CheckKishnamurthyNumber(input) // input may be a string

// after
const n = Number(input)
if (!Number.isFinite(n)) throw new TypeError('expected a finite number')
const r = CheckKishnamurthyNumber(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 = CheckKishnamurthyNumber(n)

Type guard

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

Try / catch

try {
  result = CheckKishnamurthyNumber(n)
} catch (e) {
  if (e instanceof TypeError && /not a number/i.test(e.message)) {
    // non-number input — coerce with Number() and retry
  } else throw e
}

Prevention

When it happens

Trigger: Call CheckKishnamurthyNumber('145') with a string; CheckKishnamurthyNumber(undefined) from a missing param; CheckKishnamurthyNumber([145]) passing an array; CheckKishnamurthyNumber(BigInt(145)).

Common situations: User or file input read as a string and not coerced; destructuring that yielded undefined; values from a loosely-typed API; BigInt introduced for large inputs (typeof 'bigint' not 'number').

Related errors


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