Find the Longest Word in a String - Another TypeError

Why am I getting a TypeError: cannot read property length of undefined?
Shouldn’t it be an array of characters as it states in the documentation? I was playing with the code a couple of days ago and I was able to get it to run if I specify an index such as 0 (newstr[0].length), but not if I use the i variable.

function findLongestWord(str) {
  var newstr = str.split("");
  var maxLength = 0;
  var tempLength = 1;
  //window.alert(newstr);
  //window.alert("Array length = "+ newstr.length);
  for (var i=newstr.length; i >= 0; i--){
    window.alert("Current maxLength: "+ maxLength);
    if(newstr[i].length >= tempLength){
      maxLength = tempLength;
    }
  }
  
  return newstr.join().length;
}

findLongestWord("The quick brown fox jumped over the lazy dog");

What does this do, split it up into words or letters?

Letters. but var newstr = str.split(" ") splits into words and I get the same TypeError after making that change.

and this above

IGNORE the part below and see final answer…

this part is saying stop when i >= 0 and i starts at var i=newstr.length the length of the array

and this i– should be i--

Strange. I copied and pasted directly out of the editor and it’s i-- there.

So again, why isn’t newstr[i].length valid here?

and I’m not entirely sure that’s what it was wants returned? Does it ask for the length of the longest word? or the word?

That’s doing something like taking the whole array and joining it back together and returning that joined strings length… something like that… but I’m not sure you can string those along together since one is a join on an array and the second is a length of a string. I’m not saying you can’t, I’m saying I don’t know if the syntax for js allows stringing something like that together…

anyhooooo, if the for loop worked it would still compare a letter’s length to tempLength of 1 and set maxLength to 1 as well.

Probably more hints than you need on this one.

I realize the return isn’t what it’s asking for. I like to get a skeleton of working code down and then modify it as I go. I’m not sure what I’m missing on the for loop but it’s dinner time so I’ll have to look into that tomorrow. Thanks.

IGNORE: it should be, but I’m not sure the for loop is actually doing anything if the i>=0; is in there…

EDIT: see below i = arr.length starts at a value 1 greater than the last index position of the array

eating is fundamental… i get squirelly if I don’t eat and I’m hungry when I’m trying to solve junk…

for that Q try a console.log of the newstr.length after var newstr = str.split(""); and see if you get a number there or undefined… I suspect js does something funky when you do a for starting at a value higher than your stop value… TypeError wouldn’t be the error I’d suspect there though…

The problem with your code is you start your for loop with i being set to the number 44 (the length of newstr).

Then, in your if statement, you try to reference newstr[i], which is really newstr[44]. Arrays are zero-indexed, so they start at 0 (1st element) and go up the array’s length - 1 (the last element). There is no 44th index in newstr, which is why newstr[44] is undefined and undefined does not have a length property.

Instead of making “a skeleton of working code down and then modify it as I go”, you need to first write out your algorithm and make sure you can work through an example on paper using this written algorithm before trying to write code first and hack your way through it. These are very basic coding problems, so try to learn how to document your logic first and code second. When you get to the advanced challenges, things get more complex and you have to be able to conceptually solve the problem before getting stuck in how to write the JavaScript syntax.

3 Likes

ah d’oh!

I also had to run and grab food… and voila…

it’s the reverse for loop… i === length of the string will be 1 value larger than the index of the last position… remember the array that starts at 0, so the first time through arr[i] gives undefined…

you can ignore the rest of what I said… that should fix it (arr.length-1)

Well said. I avoid doing a for loop from top to bottom if it isn’t absolutely necessary…