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.

8
  • 1
    When i == arrayWord.length, what is the value of arrayWord[i]? Commented Oct 21, 2019 at 12:07
  • 1
    You're going one iteration to far (i <= arrayWord.length), you need to change <= to < Commented Oct 21, 2019 at 12:07
  • 1
    @NickParsons, well that completely solved my problem, thank you. 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. 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) Commented Oct 21, 2019 at 14:11

2 Answers 2

4

Cannot read property 'length' of undefined comes 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

Sign up to request clarification or add additional context in comments.

1 Comment

This answer is confusing. "par.split(" ") is not able to create an array" is wrong, split always returns an array. "That's why array.length is giving you an error" no, it's not. arrayWord will always be an array provided par is a string. The issue is attempting to access arrayWord[i] when i == arrayWord.length, it will always be undefined (because length is always at least 1 greater than the last index in the array).
2

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"));

1 Comment

A good answer should explain why the OP has their issue and how your code fixes it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.