javascript - 使用javascript获取缺失数组的长度

标签 javascript typeerror

我正在尝试解决 CodeWars 上缺失数组的长度问题。 这是我的代码。

function getLengthOfMissingArray(arr) {
  let result = 0;

  if (arr === null || arr.length === 0) return 0;

  arr = arr.sort((a, b) => b.length - a.length);
  console.log(arr)

  for (let i = 0; i < arr.length; i++) {

    if (arr[i].length === 0 || arr[i] === null) return 0;

    else if (arr[i].length - arr[i + 1].length !== 1) {
      console.log(arr[i].length);
      console.log(arr[i + 1].length);

      result = arr[i].length - 1;
    }
  }
  return result;
}


console.log(getLengthOfMissingArray([
  [5, 2, 9],
  [4, 5, 1, 1],
  [1],
  [5, 6, 7, 8, 9]
]));
.as-console-wrapper { max-height: 100% !important; top: 0; }

问题是我不断收到TypeError:无法读取未定义的属性“length”console.log(arr[i + 1].length) 工作并显示 arr[i + 1].length 是 1。我对此感到非常困惑。有人可以帮我弄这个吗?谢谢!

TypeError: Cannot read property 'length' of undefined
    at getLengthOfMissingArray (/home/chrx/Documents/codeWars/Length of missing array.js:8:45)
    at Object.<anonymous> (/home/chrx/Documents/codeWars/Length of missing array.js:19:17)
    at Module._compile (internal/modules/cjs/loader.js:734:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:745:10)
    at Module.load (internal/modules/cjs/loader.js:626:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:566:12)
    at Function.Module._load (internal/modules/cjs/loader.js:558:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:797:12)
    at executeUserCode (internal/bootstrap/node.js:526:15)
    at startMainThreadExecution (internal/bootstrap/node.js:439:3)

最佳答案

您的主要问题是执行循环的最后一次迭代时下一行数组上的索引溢出:

else if (arr[i].length - arr[i + 1].length !== 1)

特别是在评估此代码时:arr[i + 1].length

但是,我对您的代码做了一些额外的修复,它们在代码中进行了解释:

function getLengthOfMissingArray(arr)
{
    // Check for all safe conditions at the start.

    if (!Array.isArray(arr) || arr.length === 0)
        return 0;

    if (arr.some(innerArr => !Array.isArray(innerArr)))
        return 0;

    // Sort mutates the array, there is no need to save it
    // again on arr variable.

    arr.sort((a, b) => b.length - a.length);

    // Start looping: to "arr.length - 1" maximum.

    for (let i = 0; i < arr.length - 1; i++)
    {
        // If find the missing length, return here, don't keep iterating.
        if (arr[i].length - arr[i + 1].length !== 1)
            return arr[i].length - 1;
    }
}

console.log("[Good Test] Missing length: ", getLengthOfMissingArray([
  [5, 2, 9],
  [4, 5, 1, 1],
  [1],
  [5, 6, 7, 8, 9]
]));

// Check samples with errors:

console.log("[Bad Test 1] Missing length: ", getLengthOfMissingArray(null));

console.log("[Bad Test 2] Missing length: ", getLengthOfMissingArray([]));

console.log("[Bad Test 3] Missing length: ", getLengthOfMissingArray([
  [5, 2, 9],
  "what?",
  [1],
  [5, 6, 7, 8, 9]
]));
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}

关于javascript - 使用javascript获取缺失数组的长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55074122/

相关文章:

javascript - 响应式网页 : SVG visualization?

javascript - 在表格中的特定行周围添加边框

python - 呈现 : 'int' object is not iterable in django template 时捕获类型错误

python - LightRun Python 代理错误 : TypeError: can only concatenate str (not "NoneType") to str

reactjs - 如何解决 TypeError : path. split is not a function

Javascript + 从变量名和字符串构建动态变量名

javascript - Sequelize findAndCountAll 分页问题

javascript - 如何解决此错误类型 'Uncaught TypeError: (...) is not a function' ?

python - "TypeError: argument of type ' 无类型 ' is not iterable"?

javascript - 对象不继承其原型(prototype)方法的问题