Build a Longest Word Finder App - Build a Longest Word Finder App

Tell us what’s happening:

I have zero clue why this is happening, it says TypeError: Cannot read properties of undefined(reading "length). I googled it and it says it appears when you use .length on an undefined variable except I am 100% sure my variables are valid and not undefined as when I manually write out the value of the variables the .length function works corretly so I have no idea why storing that value in a variable gives this error when I am 100% sure it is defined.

Your code so far

function findLongestWordLength(string){
 let  stringArray = string.split(" ")
  console.log(stringArray);
  let wordTotal = stringArray.length;
  console.log(wordTotal);
  let counter = 0;
  let charCount = 0;
  while(counter < wordTotal){
    counter++;
    console.log(stringArray[counter]);
    
    let selectWord = (stringArray[counter]);
//issue is here... except console log shows its defined.
    console.log(stringArray[counter])
    let charCount = (stringArray[counter].length); 
   
  }
}
console.log(findLongestWordLength("The quick brown fox jumped over the lazy dog"));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36

Challenge Information:

Build a Longest Word Finder App - Build a Longest Word Finder App

1 Like

This is your problem here.

The console shows ‘undefined’ is logged. Think about your loop condition and when you should increment (note - I would use a for loop here instead of a while, and then you wouldn’t have had this potential pitfall)