TheAlgorithms/JavaScript · error · Error

Input data must be type of Array

Error message

Input data must be type of Array

What it means

Thrown by uniquePaths2(obstacles) (plain Error) when !Array.isArray(obstacles). The function immediately indexes obstacles.length and obstacles[0].length, so a non-array (object, string, null, number) would either misbehave or throw a less informative TypeError later; the guard fails fast with a clear message.

Source

Thrown at Dynamic-Programming/UniquePaths2.js:43

  const matrix = []
  for (let i = 0; i < rows; i++) {
    const submatrix = []
    for (let k = 0; k < columns; k++) {
      submatrix[k] = filler
    }
    matrix[i] = submatrix
  }
  return matrix
}

/**
 * @description Return number of unique paths
 * @param {Array [][]} obstacles Obstacles grid
 * @returns {Number}
 */
const uniquePaths2 = (obstacles) => {
  if (!Array.isArray(obstacles)) {
    throw new Error('Input data must be type of Array')
  }
  // Create grid for calculating number of unique ways
  const rows = obstacles.length
  const columns = obstacles[0].length
  const grid = generateMatrix(rows, columns)
  // Fill the outermost cell with 1 b/c it has
  // the only way to reach neighbor
  for (let i = 0; i < rows; i++) {
    // If robot encounters an obstacle in these cells,
    // he cannot continue moving in that direction
    if (obstacles[i][0]) {
      break
    }
    grid[i][0] = 1
  }
  for (let j = 0; j < columns; j++) {
    if (obstacles[0][j]) {
      break

View on GitHub (pinned to 5c39e87a9a)

Solutions

  1. Pass the actual 2D array (e.g. config.grid, not config) into uniquePaths2.
  2. Validate with Array.isArray at the boundary before calling.
  3. Parse string encodings ('010\n101') into a number[][] first.
  4. Default undefined inputs to an empty array or fail with a domain-specific message.

Example fix

// before
const paths = uniquePaths2(payload) // throws if payload is an object, not its grid

// after
const grid = Array.isArray(payload) ? payload : payload?.grid
if (!Array.isArray(grid)) throw new TypeError('obstacles must be number[][]')
const paths = uniquePaths2(grid)
Defensive patterns

Strategy: type-guard

Validate before calling

function safeUniquePaths2(obstacles) {
  const grid = Array.isArray(obstacles) ? obstacles : obstacles?.grid
  if (!Array.isArray(grid)) {
    throw new TypeError('obstacles must be a number[][] array')
  }
  return uniquePaths2(grid)
}

Type guard

const isGrid = (v) => Array.isArray(v) && v.every((row) => Array.isArray(row))

Try / catch

try {
  return uniquePaths2(obstacles)
} catch (e) {
  if (e instanceof Error && /type of array/i.test(e.message)) {
    return uniquePaths2(obstacles.grid)
  }
  throw e
}

Prevention

When it happens

Trigger: uniquePaths2({rows: 3, cols: 3}); uniquePaths2('010'); uniquePaths2(null); uniquePaths2(undefined).

Common situations: Passing a JSON-deserialized object whose grid lives under a property rather than at the top level; passing a flattened string representation; undefined from a missing config field.

Related errors


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