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 ofarrayWord[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
2 Answers
Reset to default 4Cannot 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"));