Basic Algorithm Scripting - Find the Longest Word in a String

Tell us what’s happening:
Been banging my head for an hour now trying to figure out why it’s returning the length of the entire string and not separating the words out into an array. I thought that the split function did that automatically?

A push in the right direction would be most helpful :slight_smile:

Your code so far

function findLongestWordLength(str) {
  
  let strSplit = str.split(" ");
  let longest = 0;
  for(let i = 0; i < str.split.length; i++) {
    if(strSplit[i].length > longest) {
    longest = strSplit[i].length;
    }
}
  return str.length;
}

findLongestWordLength("The quick brown fox jumped over the lazy dog");
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; rv:107.0) Gecko/20100101 Firefox/107.0

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

Link to the challenge:

What does this line say you are returning?

I don’t think this means what you want it to mean.

Thanks Jeremy. I’ll have to come back to it after work tonight and either think it over or start over with the code.

You shouldn’t need to start over. Your logic is basically correct but you have two bugs. I was able to fix your code by fixing those two spots.

I’m getting closer. Now it’s returning the amount of words in the string. Oh boy. :laughing: I’m soldiering on.

edit
Took another hour but I finally did it! Sheesh! I make things waaay more complicated than I should I swear.

1 Like