TheAlgorithms/JavaScript · error · TypeError

Argument is not a number.

Error message

Argument is not a number.

What it means

The ReverseNumber function reverses the digits of a number mathematically using modulo and division. The guard rejects non-number types because the arithmetic would fail or coerce incorrectly. Important caveat: the function only handles positive numbers (the while loop checks number > 0), so negative numbers silently return 0 without error.

Source

Thrown at Maths/ReverseNumber.js:13

/*
    Problem statement and Explanation : https://medium.com/@ManBearPigCode/how-to-reverse-a-number-mathematically-97c556626ec6
*/

/**
 * ReverseNumber return the reversed value of the given number.
 * @param {Number} n any digit number.
 * @returns `Number` n reverse in reverse.
 */
const ReverseNumber = (number) => {
  // firstly, check that input is a number or not.
  if (typeof number !== 'number') {
    throw new TypeError('Argument is not a number.')
  }
  // A variable for storing the reversed number.
  let reverseNumber = 0
  // Iterate the process until getting the number is 0.
  while (number > 0) {
    // get the last digit of the number
    const lastDigit = number % 10
    // add to the last digit to in reverseNumber
    reverseNumber = reverseNumber * 10 + lastDigit
    // reduce the actual number.
    number = Math.floor(number / 10)
  }
  return reverseNumber
}

export { ReverseNumber }

View on GitHub (pinned to 5c39e87a9a)

Solutions

  1. Pass a number primitive to ReverseNumber.
  2. Parse strings with Number() or parseInt() before calling.
  3. Be aware that negative numbers silently return 0; handle the sign separately.

Example fix

// before
ReverseNumber(inputEl.value) // inputEl.value is a string
// after
ReverseNumber(Number(inputEl.value))
Defensive patterns

Strategy: type-guard

Validate before calling

if (typeof number !== 'number') {
  throw new TypeError('Argument must be a number')
}
ReverseNumber(number)

Type guard

const isNonNegativeNumber = (n) => typeof n === 'number' && n >= 0

Prevention

When it happens

Trigger: Calling ReverseNumber("123"), ReverseNumber(null), or ReverseNumber([1,2,3]). Any non-number type triggers this error.

Common situations: String numbers from HTML form inputs, parsed JSON, CLI arguments, or values extracted from Maps/Sets that lost their type.

Related errors


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