TheAlgorithms/JavaScript · error · TypeError

Argument is not a string

Error message

Argument is not a string

What it means

Thrown as a TypeError by checkExceeding() when str is not a string. The function calls str.toUpperCase().replace(...) and destructures characters by index, all requiring a string receiver. It checks whether gaps between adjacent ASCII codes are monotonically non-decreasing.

Source

Thrown at String/CheckExceeding.js:11

/**
 * @function checkExceeding
 * @description - Exceeding words are words where the gap between two adjacent characters is increasing. The gap is the distance in ascii
 * @param {string} str
 * @returns {boolean}
 * @example - checkExceeding('delete') => true, ascii difference - [1, 7, 7, 15, 15] which is incremental
 * @example - checkExceeding('update') => false, ascii difference - [5, 12, 3, 19, 15] which is not incremental
 */
const checkExceeding = (str) => {
  if (typeof str !== 'string') {
    throw new TypeError('Argument is not a string')
  }

  const upperChars = str.toUpperCase().replace(/[^A-Z]/g, '') // remove all from str except A to Z alphabets

  const adjacentDiffList = []

  for (let i = 0; i < upperChars.length - 1; i++) {
    // destructuring current char & adjacent char by index, cause in javascript String is an object.
    const { [i]: char, [i + 1]: adjacentChar } = upperChars

    if (char !== adjacentChar) {
      adjacentDiffList.push(
        Math.abs(char.charCodeAt() - adjacentChar.charCodeAt())
      )
    }
  }

  for (let i = 0; i < adjacentDiffList.length - 1; i++) {

View on GitHub (pinned to 5c39e87a9a)

Solutions

  1. Coerce: checkExceeding(String(word)).
  2. Guard at the boundary with typeof.
  3. Ensure the data source always yields strings.

Example fix

// before
checkExceeding(value)

// after
checkExceeding(String(value ?? ''))
Defensive patterns

Strategy: type-guard

Validate before calling

function safeCheckExceeding(str) {
  if (typeof str !== 'string') {
    throw new TypeError('Expected a string')
  }
  return checkExceeding(str)
}

Type guard

function isString(v) {
  return typeof v === 'string'
}

Try / catch

try {
  checkExceeding(word)
} catch (e) {
  if (e instanceof TypeError && e.message === 'Argument is not a string') {
    return checkExceeding(String(word))
  }
  throw e
}

Prevention

When it happens

Trigger: Calling checkExceeding(null), checkExceeding(12345), checkExceeding(['d','e','l','e','t','e']), or checkExceeding(undefined).

Common situations: Passing a numeric sequence instead of a word; absent JSON fields; values routed through a numeric pipeline.

Related errors


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