Find the Longest Word in a String. Check solution pls

Tell us what’s happening:
Describe your issue in detail here.
Hello everyone!
I dont know if i missed a chapter in the course but every solution in the help section use the slipt method and i dont recall seeing it anywhere. My code checks but i cant help to think theres a diferent way to do it besides the slipt method and this code.
Check my code pls any suggestion is welcome. Thank u!

  **Your code so far**

function findLongestWordLength(str) {
let arr=[];
let newArr=[...str];
let count= 0;
for(let i=0; i <= newArr.length; i++){
  if(newArr[i]===' '|| i == newArr.length){
    let word = newArr.slice(count, i);
    arr.push(word);
    count=i+1;
   
  }
}
let longest = arr[0];
for(let j=0; j < arr.length; j++){
  longest.length < arr[j].length ? longest = arr[j] : longest = longest
}
 return longest.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/96.0.4664.110 Safari/537.36 Edg/96.0.1054.62

Challenge: Find the Longest Word in a String

Link to the challenge:

Split is the best way to do this.

Your logic is very difficult to follow because your variable names don’t correspond to what the variables are being used for. You can greatly simplify your code by only keeping track of the length of the longest word each time you find a space instead of splicing out an array of words that you never actually need.

2 Likes

split() comes later in the section “functional programming”. But you are right, split() is not the only way to go about this problem.

However your algorithm is a bit on the more convoluted/inefficient side, with a few unconventional choices of doing things.

instead of first looping and finding all words and pushing into an array, then looping and finding the longest in the array, how about:
keeping track of the longest word length and current word length, and every time you encounter a space, compare the two to see if you found a longer word, finally doing one more comparison when you reach end of string.

your variable names are not very descriptive of their purpose, making your code unnecessarily difficult to go through

There’s no need to destructure the string into array, you can access string chars the same way you do array elements, you can even do slice()

the ternary condition in the second for loop conventionally would be written as:
longest=longest.length < arr[j].length ? arr[j] : longest

2 Likes

Thank u for replying i took both of the suggestions and this is what i came up with
Its definetly better!
Sorry for the var names i still need to work on them.
Im not sure about the cutCount var still

function findLongestWordLength(str) {
let wordCount= 0;
let cutCount= 0;
for(let i=0; i <= str.length; i++){
  if(str[i]===' '|| i == str.length){
    wordCount = wordCount < (i - cutCount) ? (i - cutCount) :   wordCount;
    cutCount = i+1;
  }
}
 return wordCount
}

I don’t really understand the subtraction with i, it seems overly complicated. Instead you could just reset your current word length counter to 0 whenever you hit a space, and tick it up by 1 each loop otherwise.

For variable names how about longestWordLength and currentWordLength?

Also, I would prefer Math.max() for the readability over using the ternary expression.


But your solution works fine! Good job! Definitely an improvement in readability over your first attempt.

1 Like

I like this better. I’d change two things though:

wordCount isn’t a count

I think Max is clearer and more conventional than a ternary here.

1 Like