{"record":{"id":"dd3595040dd21630","repo":"TheAlgorithms/JavaScript","slug":"input-data-must-be-type-of-array","errorCode":null,"errorMessage":"Input data must be type of Array","messagePattern":"Input data must be type of Array","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"Dynamic-Programming/UniquePaths2.js","lineNumber":43,"sourceCode":"  const matrix = []\n  for (let i = 0; i < rows; i++) {\n    const submatrix = []\n    for (let k = 0; k < columns; k++) {\n      submatrix[k] = filler\n    }\n    matrix[i] = submatrix\n  }\n  return matrix\n}\n\n/**\n * @description Return number of unique paths\n * @param {Array [][]} obstacles Obstacles grid\n * @returns {Number}\n */\nconst uniquePaths2 = (obstacles) => {\n  if (!Array.isArray(obstacles)) {\n    throw new Error('Input data must be type of Array')\n  }\n  // Create grid for calculating number of unique ways\n  const rows = obstacles.length\n  const columns = obstacles[0].length\n  const grid = generateMatrix(rows, columns)\n  // Fill the outermost cell with 1 b/c it has\n  // the only way to reach neighbor\n  for (let i = 0; i < rows; i++) {\n    // If robot encounters an obstacle in these cells,\n    // he cannot continue moving in that direction\n    if (obstacles[i][0]) {\n      break\n    }\n    grid[i][0] = 1\n  }\n  for (let j = 0; j < columns; j++) {\n    if (obstacles[0][j]) {\n      break","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Dynamic-Programming/UniquePaths2.js#L25-L61","documentation":"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.","triggerScenarios":"uniquePaths2({rows: 3, cols: 3}); uniquePaths2('010'); uniquePaths2(null); uniquePaths2(undefined).","commonSituations":"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.","solutions":["Pass the actual 2D array (e.g. config.grid, not config) into uniquePaths2.","Validate with Array.isArray at the boundary before calling.","Parse string encodings ('010\\n101') into a number[][] first.","Default undefined inputs to an empty array or fail with a domain-specific message."],"exampleFix":"// before\nconst paths = uniquePaths2(payload) // throws if payload is an object, not its grid\n\n// after\nconst grid = Array.isArray(payload) ? payload : payload?.grid\nif (!Array.isArray(grid)) throw new TypeError('obstacles must be number[][]')\nconst paths = uniquePaths2(grid)","handlingStrategy":"type-guard","validationCode":"function safeUniquePaths2(obstacles) {\n  const grid = Array.isArray(obstacles) ? obstacles : obstacles?.grid\n  if (!Array.isArray(grid)) {\n    throw new TypeError('obstacles must be a number[][] array')\n  }\n  return uniquePaths2(grid)\n}","typeGuard":"const isGrid = (v) => Array.isArray(v) && v.every((row) => Array.isArray(row))","tryCatchPattern":"try {\n  return uniquePaths2(obstacles)\n} catch (e) {\n  if (e instanceof Error && /type of array/i.test(e.message)) {\n    return uniquePaths2(obstacles.grid)\n  }\n  throw e\n}","preventionTips":["Pass the grid array itself, not a wrapper object.","Validate with Array.isArray at the boundary.","Parse string encodings into number[][] before calling.","Default undefined config to an empty array or fail with a clear message."],"tags":["dynamic-programming","grid","type-guard","input-shape"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}