Basic Algorithm Scripting - Find the Longest Word in a String

Tell us what’s happening:
Can anyone tell me why my code is not working? My function is returning 0.

I turned the string into an array of all the individual words that I can cycle through with a for loop. I logged the output to the console to confirm that part is working.

The issue seems to be with my for loop… My variable longestWord is never getting overwritten. :frowning:
Thank you!!

Your code so far

function findLongestWordLength(str) {
  let wordRegex = /\w+/g;
  let array = str.match(wordRegex);
  let longestWord = "";
  
  for (let i=0; i<array.lenth; i++){
    if (array[i].length > longestWord.length){
      longestWord = array[i];
    }
  }

  return longestWord.length;
}

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/115.0.0.0 Safari/537.36 Edg/115.0.1901.183

Challenge: Basic Algorithm Scripting - Find the Longest Word in a String

Link to the challenge:

Check this line carefully

AAAHHH! Thank you!! And I just finished the debugging lesson too! :rofl::rofl:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.