【问题标题】:TypeError: Cannot read properties of undefined (reading '0'). in javascript类型错误:无法读取未定义的属性(读取 \'0\')。在javascript中
【发布时间】:2023-01-08 21:24:39
【问题描述】:

为什么会出现此错误

我的代码是

/**
 * @param {number[][]} points
 * @return {number}
 */
var maxPoints = function(points) {
    n=0
    for(let i=0;i<=points.length;i++){
    let x=points[i]
    const y=points[i+1]
    let dx=y[0]-x[0]
    let dy=y[1]-x[1]
    m=dy/dx
    b = y[1]-m*y[0]
    newy=m*y[0]+b
        if(newy==y[1]){
            n++
        }
        console.log(y)
    }
    return(n)
};

错误

Line 10 in solution.js
    let deltax=y[0]-x[0]
                ^
TypeError: Cannot read properties of undefined (reading '0')
    Line 10: Char 17 in solution.js (maxPoints)
    Line 31: Char 19 in solution.js (Object.<anonymous>)
    Line 16: Char 8 in runner.js (Object.runner)
    Line 22: Char 26 in solution.js (Object.<anonymous>)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47

问题是找到从给定点数组绘制直线的最大点数 如何解决这个错误

【问题讨论】:

    标签: javascript arrays arraylist graph


    【解决方案1】:

    将循环条件更改为i&lt;points.length

    【讨论】:

      猜你喜欢
      • 2021-10-01
      • 2023-02-26
      • 2022-01-06
      • 2018-04-29
      • 2022-12-08
      • 2022-12-01
      • 2022-12-21
      • 2022-12-24
      • 2022-12-25
      相关资源
      最近更新 更多