TheAlgorithms/JavaScript · error · Error

Invalid input, please provide valid number

Error message

Invalid input, please provide valid number

What it means

Thrown by `countNumberWordLength(number)` (Project Euler #17) when `Number.isNaN(parseInt(number))` is true - i.e. when the input cannot be parsed as an integer at all (e.g. 'abc', null becomes NaN). Be aware `parseInt('12abc')` returns 12 (passes), and `parseInt(12.5)` returns 12, so partial-parse and float inputs slip through this guard.

Source

Thrown at Project-Euler/Problem017.js:100

}

/**
 * Function responsible for calculating total number word length
 * for provided input number
 * Validation is performed for input
 * Loop is executed to find total word length for given number range
 * starting from 1
 *
 *
 * @param {number} number
 * @returns {number}
 */
const countNumberWordLength = (number) => {
  let count = 0

  // Not a number check
  if (Number.isNaN(parseInt(number))) {
    throw new Error('Invalid input, please provide valid number')
  }

  // Number should be greater than 1
  if (number < 1) {
    throw new Error('Please provide number greater that 1')
  }

  // Loop to calculate word length by calling {@link numberToWord}
  for (let i = 1; i <= number; i++) {
    count += numberToWordLength(i)
  }

  // return final count for number word length
  return count
}

export { countNumberWordLength }

View on GitHub (pinned to 5c39e87a9a)

Solutions

  1. Validate strictly upstream: `Number.isInteger(Number(input))` rather than relying on parseInt.
  2. Strip/verify the input is purely numeric with `/^\d+$/` before calling.
  3. Reject empty/null/undefined explicitly before forwarding.

Example fix

// before
countNumberWordLength(rawInput) // rawInput could be '' or 'abc'

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

Strategy: validation

Validate before calling

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

Type guard

const isIntegerLike = (x) => Number.isInteger(Number(x))

Prevention

When it happens

Trigger: Call `countNumberWordLength('abc')`, `countNumberWordLength(null)`, `countNumberWordLength({})`. Numeric strings like '5' pass; partially-numeric strings like '5x' also pass.

Common situations: Empty form fields forwarded without validation, free-text user input, or values from a CSV where a column has mixed types.

Related errors


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