最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to fix TypeError: Cannot read property 'length' of undefined in JavaScript? - Stack Overflow

programmeradmin1浏览0评论

I am trying to solve a very easy challenge about finding the longest word in a string. This is the code:

function find(par) {
    let arrayWord = par.split(" ");
    let longestWord = "";
    for (let i = 0; i <= arrayWord.length; i++) {
        if (longestWord.length < arrayWord[i].length) {
            longestWord = arrayWord[i]
        }
    }
    return longestWord;
}
find("Find the longest word");

I would need help understanding why I am getting this error:

Uncaught TypeError: Cannot read property 'length' of undefined at find (:5:47) at :11:1 find @ VM959:5 (anonymous) @ VM959:11

thank you.

I am trying to solve a very easy challenge about finding the longest word in a string. This is the code:

function find(par) {
    let arrayWord = par.split(" ");
    let longestWord = "";
    for (let i = 0; i <= arrayWord.length; i++) {
        if (longestWord.length < arrayWord[i].length) {
            longestWord = arrayWord[i]
        }
    }
    return longestWord;
}
find("Find the longest word");

I would need help understanding why I am getting this error:

Uncaught TypeError: Cannot read property 'length' of undefined at find (:5:47) at :11:1 find @ VM959:5 (anonymous) @ VM959:11

thank you.

Share Improve this question edited Oct 21, 2019 at 12:05 Nick Parsons 51k6 gold badges57 silver badges75 bronze badges asked Oct 21, 2019 at 12:04 Andrea D_Andrea D_ 2,1415 gold badges21 silver badges45 bronze badges 8
  • 1 When i == arrayWord.length, what is the value of arrayWord[i]? – RobG Commented Oct 21, 2019 at 12:07
  • 1 You're going one iteration to far (i <= arrayWord.length), you need to change <= to < – Nick Parsons Commented Oct 21, 2019 at 12:07
  • 1 @NickParsons, well that pletely solved my problem, thank you. – Andrea D_ Commented Oct 21, 2019 at 14:00
  • 1 @IceMetalPunk, thank you I totally understood now. I didn't take into consideration that the last item of an array is = array.length - 1, therefore adding a = next to the < would iterate for another element that is not in the array. – Andrea D_ Commented Oct 21, 2019 at 14:09
  • 1 @chandukomati so if I understood it right, the error length is undefined it is because I try to retrieve the length of a nonexisting array element (which in this case is given by the = operator next to the < operator) – Andrea D_ Commented Oct 21, 2019 at 14:11
 |  Show 3 more ments

2 Answers 2

Reset to default 4

Cannot read property 'length' of undefined es when it is not able to find variable of certain type(In your case a string) to call the function length. In your case arrayWord[i].length is not a proper string for the last condition of your check as there is no element arrayWord[arrayWord.length] present in the array. That's why arrayWord[i].length is giving you an error for your last iteration. Just change i <= arrayWord.length to i < arrayWord.length

function find(par) {
  let arrayWord = par.split(" ");
  let longestWord = "";
  for (let i = 0; i <arrayWord.length; i++) {
    if (longestWord.length < arrayWord[i].length) {
      longestWord = arrayWord[i]
    }
  }
  return longestWord;
}

Edits: Changes made as suggested by RobG

Just change condition <= to < and try

function find(par) {
    let arrayWord = par.split(" ");
    let longestWord = "";
    for (let i = 0; i < arrayWord.length; i++) {
        if (longestWord.length < arrayWord[i].length) {
            longestWord = arrayWord[i]
        }
    }
    return longestWord;
}

console.log(find("Find the longest word"));

发布评论

评论列表(0)

  1. 暂无评论