TheAlgorithms/JavaScript · error · TypeError

Input must be a integer number

Error message

Input must be a integer number

What it means

Thrown by `isPalindromeIntegerNumber(x)` when `typeof x !== 'number'`. Despite the message saying 'integer', the guard only checks the `number` type - floats pass and are then rejected with a `return false`, not an exception. NaN is typeof 'number' so it slips through and returns false without throwing.

Source

Thrown at Maths/isPalindromeIntegerNumber.js:11

/**
 * @function isPalindromeIntegerNumber
 * @param { Number } x
 * @returns {boolean} - input integer is palindrome or not
 *
 * time complexity : O(log_10(N))
 * space complexity : O(1)
 */
export function isPalindromeIntegerNumber(x) {
  if (typeof x !== 'number') {
    throw new TypeError('Input must be a integer number')
  }
  // check x is integer
  if (!Number.isInteger(x)) {
    return false
  }

  // if it has '-' it cannot be palindrome
  if (x < 0) return false

  // make x reverse
  let reversed = 0
  let num = x

  while (num > 0) {
    const lastDigit = num % 10
    reversed = reversed * 10 + lastDigit
    num = Math.floor(num / 10)
  }

View on GitHub (pinned to 5c39e87a9a)

Solutions

  1. Coerce with `Number(x)` and ensure the result is a finite integer before calling.
  2. For string inputs, validate `/^-?\d+$/` first.
  3. Be aware NaN and floats do not throw - they silently return false - so add an upstream NaN guard if that matters.

Example fix

// before
isPalindromeIntegerNumber(input) // input may be a string '121'

// after
const n = Number(input)
if (!Number.isInteger(n)) throw new TypeError('input must be an integer')
isPalindromeIntegerNumber(n)
Defensive patterns

Strategy: type-guard

Validate before calling

const n = Number(x)
if (!Number.isInteger(n)) {
  throw new TypeError('input must be an integer')
}
isPalindromeIntegerNumber(n)

Type guard

const isIntegerNumber = (x) => typeof x === 'number' && Number.isInteger(x)

Prevention

When it happens

Trigger: Call `isPalindromeIntegerNumber('121')`, `isPalindromeIntegerNumber(null)`, `isPalindromeIntegerNumber(BigInt(121))`, `isPalindromeIntegerNumber(undefined)`. Floats like `12.1` do NOT throw - they return false.

Common situations: URL/form input parsed as string, BigInt from a parser, or a value pulled from JSON where large integers were stringified.

Related errors


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