You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

函数报错‘Cannot read property '0' of undefined’的排查与修复

问题:矩阵幸运数函数出现「Cannot read property '0' of undefined」报错

需求目标

返回矩阵中既是所在行最小值,又是所在列最大值的数(幸运数)。

我的代码

function luckyNumbers(matrix) {
  for (let i = 0; i < matrix.length; i++) {
      for (let j = 0; j < matrix[i].length; j++) {
          let ele = matrix[i][j];
          if (minInRow(ele) === maxInColumn(ele)) {
             return ele;
          }
      }
  }
};

matrix = [[ 5,  9, 21],
          [ 9, 19,  6],
          [12, 14, 15]]

console.log(luckyNumbers(matrix)); // expected output - [12]

matrix = [[ 5, 10,  8,  6],
          [10,  2,  7,  9],
          [21, 15, 19, 10]]

console.log(luckyNumbers(matrix)); // expected output - [10]

// --------------------- MIN_IN_ROW FUNCTION ---------------------------
function minInRow(arr) {
    let minNum = arr[0][0];
  for (let i = 0; i < arr.length; i++) {
      
      for (let j = 0; j < arr[i].length; j++) {
          let ele = arr[i][j];
          
          if (ele < minNum) {
              minNum = ele;
          }
      }
  }
  return minNum;
};

/* console.log(minInRow([[6, 5, 11],
                      [8, 7, 3 ], 
                      [9, 12, 1]])); */

// --------------- MAX_IN_COLUMN FUNCTION ---------------------------

function maxInColumn(arr) {
    let maxNum = arr[0][0];
    for (let i = 0; i < arr.length; i++) {
        
        for (let j = 0; j < arr[i].length; j++) {
            let ele = arr[i][j];
            
            if (ele > maxNum) {
                maxNum = ele;
            }
        }
    }
    return maxNum;
};

/* console.log(maxInColumn([[3,  5, 6],
                         [2, 20, 30],
                         [9, 10, 2]])); */
// -----------------------------------------------------------------

报错信息

let minNum = arr[0][0];
                       ^
TypeError: Cannot read property '0' of undefined
    at minInRow (/tmp/uEg6djEUrs.js:30:24)
    at luckyNumbers (/tmp/uEg6djEUrs.js:5:15)
    at Object.<anonymous> (/tmp/uEg6djEUrs.js:16:13)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
    at startup (internal/bootstrap/node.js:283:19)

问题分析与修复

核心错误点

  1. 参数传递完全错误:在luckyNumbers里,你把单个数值ele传给了minInRow和maxInColumn,但这两个函数原本是为接收数组/矩阵写的。当单个数值被传入后,arr[0]自然是undefined,读取arr[0][0]就会触发报错。
  2. 辅助函数逻辑偏离需求:原minInRow是找整个矩阵的最小值,不是当前元素所在行的最小值;maxInColumn是找整个矩阵的最大值,不是当前元素所在列的最大值。

修复后的代码(简化版)

function luckyNumbers(matrix) {
  const lucky = [];
  // 先预存每一行的最小值,避免重复计算
  const rowMins = matrix.map(row => Math.min(...row));

  for (let i = 0; i < matrix.length; i++) {
    for (let j = 0; j < matrix[i].length; j++) {
      const current = matrix[i][j];
      // 先判断是不是当前行的最小值,不是就跳过
      if (current !== rowMins[i]) continue;
      
      // 再判断是不是当前列的最大值
      let isColMax = true;
      for (let k = 0; k < matrix.length; k++) {
        if (matrix[k][j] > current) {
          isColMax = false;
          break;
        }
      }

      if (isColMax) lucky.push(current);
    }
  }
  return lucky;
};

// 测试用例1
matrix = [[ 5,  9, 21],
          [ 9, 19,  6],
          [12, 14, 15]]
console.log(luckyNumbers(matrix)); // 输出: [12]

// 测试用例2
matrix = [[ 5, 10,  8,  6],
          [10,  2,  7,  9],
          [21, 15, 19, 10]]
console.log(luckyNumbers(matrix)); // 输出: [10]

另一种修复方式(保留原辅助函数结构)

如果想保留你原本的辅助函数思路,可以调整参数传递和辅助函数逻辑:

function luckyNumbers(matrix) {
  const lucky = [];
  for (let i = 0; i < matrix.length; i++) {
      for (let j = 0; j < matrix[i].length; j++) {
          let ele = matrix[i][j];
          // 传递当前行给minInRow,传递矩阵+列索引给maxInColumn
          if (minInRow(matrix[i]) === ele && maxInColumn(matrix, j) === ele) {
             lucky.push(ele);
          }
      }
  }
  return lucky;
};

// 修正:接收单行数组,找该行最小值
function minInRow(row) {
    let minNum = row[0];
    for (let num of row) {
        if (num < minNum) minNum = num;
    }
    return minNum;
};

// 修正:接收矩阵和列索引,找该列最大值
function maxInColumn(matrix, colIndex) {
    let maxNum = matrix[0][colIndex];
    for (let row of matrix) {
        if (row[colIndex] > maxNum) maxNum = row[colIndex];
    }
    return maxNum;
};

// 测试用例
matrix = [[ 5,  9, 21],
          [ 9, 19,  6],
          [12, 14, 15]]
console.log(luckyNumbers(matrix)); // 输出: [12]

matrix = [[ 5, 10,  8,  6],
          [10,  2,  7,  9],
          [21, 15, 19, 10]]
console.log(luckyNumbers(matrix)); // 输出: [10]

内容的提问来源于stack exchange,提问作者Ali

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 03:10:56