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
- Coerce with `Number(x)` and ensure the result is a finite integer before calling.
- For string inputs, validate `/^-?\d+$/` first.
- 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
- Coerce string input with Number() and check Number.isInteger.
- Be aware the function silently returns false for floats and NaN - do not rely on it throwing.
- The error message says 'integer' but the throw only checks 'number' - validate stricter upstream.
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
- Index cannot be a Decimal
- Expected integer N and finite a, b
- Expected integer N and finite a, b
- Expected a number, received ${typeof num}
- Expected a number, received ${typeof precision}
AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13).
Data as JSON: /api/errors/fd360161bd1cd64d.
Report an issue: GitHub.