TheAlgorithms/JavaScript · error · Error

Input data must be numbers

Error message

Input data must be numbers

What it means

The intToBase function converts an integer to its string representation in a given base using Horner's method (repeated modulo and division). Both parameters must be JavaScript number primitives; strings or other types are rejected because the arithmetic operations would produce incorrect results via silent type coercion.

Source

Thrown at Maths/IntToBase.js:17

/**
 * @function intToBase
 * @description Convert a number from decimal system to another (till decimal)
 * @param {Number} number Number to be converted
 * @param {Number} base Base of new number system
 * @returns {String} Converted Number
 * @see [HornerMethod](https://en.wikipedia.org/wiki/Horner%27s_method)
 * @example
 * const num1 = 125 // Needs to be converted to the binary number system
 * gornerScheme(num, 2); // ===> 1111101
 * @example
 * const num2 = 125 // Needs to be converted to the octal number system
 * gornerScheme(num, 8); // ===> 175
 */
const intToBase = (number, base) => {
  if (typeof number !== 'number' || typeof base !== 'number') {
    throw new Error('Input data must be numbers')
  }
  // Zero in any number system is zero
  if (number === 0) {
    return '0'
  }
  let absoluteValue = Math.abs(number)
  let convertedNumber = ''
  while (absoluteValue > 0) {
    // Every iteration last digit is taken away
    // and added to the previous one
    const lastDigit = absoluteValue % base
    convertedNumber = lastDigit + convertedNumber
    absoluteValue = Math.trunc(absoluteValue / base)
  }
  // Result is whether negative or positive,
  // depending on the original value
  if (number < 0) {
    convertedNumber = '-' + convertedNumber

View on GitHub (pinned to 5c39e87a9a)

Solutions

  1. Ensure both arguments are number primitives (typeof === 'number').
  2. If reading from user input, parse with Number() or parseInt() before calling.
  3. Wrap the call in a type-check helper that converts or rejects non-number inputs.

Example fix

// before
intToBase(inputEl.value, 2) // inputEl.value is a string
// after
intToBase(Number(inputEl.value), 2)
Defensive patterns

Strategy: type-guard

Validate before calling

if (typeof number !== 'number' || typeof base !== 'number') {
  throw new TypeError('Both arguments must be numbers')
}
intToBase(number, base)

Type guard

const areNumbers = (n, b) => typeof n === 'number' && typeof b === 'number'

Prevention

When it happens

Trigger: Calling intToBase("125", 2), intToBase(125, "2"), or passing any non-number type for either argument. Values from HTML input fields or parsed JSON strings are the most common culprits.

Common situations: Values read from HTML form inputs (always strings), JSON where numbers were serialized as quoted strings, CLI arguments (always strings), or config values loaded as strings.

Related errors


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