Find the Longest Word in a String: undefined is not an object

**Tell us what’s happening:

Hi there, I’m trying to solve this puzzle and I think im pretty much on the way. In my console the highest number is shown, so I would expect it to be fine when I return variable “highest”. But I get the following error:

undefined is not an object (evaluating 'array[i].length')

Whats going on here?

**Your code so far

function findLongestWordLength(str) {

let array = str.split(" ");
let arrayCount = [];
let highest = 0;

for (let i = 0; i <= array.length; i++) {
arrayCount.push(array[i].length);
highest = Math.max(...arrayCount);
console.log(highest);
}
return highest;
}

findLongestWordLength("The dsfad dasfdsffadefasdfedf fsd fsfdvcsrs");

Link to the challenge:

It’s trying to tell you that array[i] is undefined, therefor it cannot treat it like an object, i.e., it cannot get a property on it. You can only try to access properties on objects (including arrays, functions, etc) and primitives that have associated object wrappers (string, number, boolean). But undefined and null are primitives without object wrappers so it will throw and error if you look for a property.

I’ll give you a hint - it’s not the first time that that is failing, but the last time. You need to examine your for loop, I think. You made a very common error with for loops.

Thanks for the cryptic hint :wink:

The code seams to work, except for 1 sentence:

May the force be with you

In my code the outcome is 5, which is the expected answer, It says its not right.

let arrayCount = [];
let highest = 0;

function findLongestWordLength(str) {

let array = str.split(" ");
for (let i = 0; i < array.length; i++) {
arrayCount.push(array[i].length);
highest = Math.max(...arrayCount);
console.log(highest);
}
  return highest;
}


findLongestWordLength("May the force be with you"); 

In your second code you changed the scope of the arrayCount variable. The tests are calling the function back to back like this:

findLongestWordLength("The quick brown fox jumped over the lazy dog");
findLongestWordLength("May the force be with you");

If you throw in a console.log(arrayCount); at the end your log will look like this:

3,5,5,3,6,4,3,4,3,3,3,5,2,4,3

It has all the word length counts in it from the first call. So, Math.max(...arrayCount) correctly returns 6. But that is not the result you want.

Right, you can’t use global variables in these tests - they don’t get cleared out for each test.

Ditch equality sign in for loop i <= array.length or add minus to it at a back i <= array.length-1.

I couldn’t figure it out, so I decided to try something different.

let highest = 0;
let arrayOfWords = [];
let arrayOfNumbers;

function findLongestWordLength(str) {

arrayOfWords = str.split(' ');

for (let i = 0; i < arrayOfWords.length; i++ ){

if (arrayOfWords[i].length > highest) {
  highest = arrayOfWords[i].length;
} 
}

console.log(highest);
return highest;
  
}

findLongestWordLength("May the force be with you")

And to my surprise I have the exact same problem with this different code.
Also here my console gives the right answer, but the same test gives the same problem

Never mind, found the solution. Still not sure if its logical, but putting highest in the function solved the problem

That is what I/we we were trying to tell you. You still had the same problem with your new code because your variable highest was global. It needs to be local to your function. This makes your function reusable when your variables are local. The tests “reuse” your function by calling it repeatedly with different parameters. So when the function is called a second time highest is not 0, it it the number of the longest word the first time the function was called. If the longest word in the first call was 9, and the longest word in the second call is 6, your function returns 9. If the variable is local, highest is 0 before your for loop starts on the second call rather than 9, so now 6 would be higher than 0 and your function would return 6 on the second call. Also, your other variables should be local for best practices.

It took a while for me to get it :slight_smile: . Thanks for the help!

Hi @S-Gr-Xrx,
this simpler code worked for me:

        function findLongestWordLength(str) {
            let stir = str.split(" ") 
            let sr = []
            for (let st in stir){
                sr.push(stir[st].length)
            }
            return Math.max(...sr);
        }
        findLongestWordLength("The quick brown fox jumped over the lazy dog");

hope this helps.
Paul