Find the Longest Word: Why Is My For Loop Not Working?

Tell us what’s happening:
So I’m trying to solve this problem and here’s my general idea so far:

I selected each word and put them into an array using a regular expression. Then I tried to create a for loop that would take each word, starting with the first 2 in the array, and compare each one, see which was bigger, than kick out the smaller one and continue the process until only one word was left. This isn’t working and I would like someone to tell me what my code is actually saying to do, since I’m not achieving my desired result of only word left in the array after the for loop has run.

  **Your code so far**

let regex = /[a-z]+/gi

function findLongestWordLength(str) {
str = (str.match(regex));
for (let i = 0 ; i < (str.length - 1); i++){
  for (let j = 1 ; j < str.length; j++){
    if (str[i].length <= str[j].length){
    str.shift();
    } else {
      str.splice(j,1)
      
    }
  }
}
  return str
}






findLongestWordLength("The quick brown fox jumped over the lazy dog");


  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.2 Safari/605.1.15

Challenge: Find the Longest Word in a String

Link to the challenge:

Use this => instead of <=, I think this might be the mistake in your code.

The way you’re splitting words using match() certainly works, but I would suggest you check out the string method split(), which is a little more idiomatic for this sort of thing.

Step back for a moment, you need to find the word with the longest length. You have an array of word strings.

Imagine you, as a human being, have to find the longest word in an entire book. It’s massive, so you have to go one by one through every single word. You have a pen and paper. What would you write down to keep track of the longest word?

I tried that and it doesn’t fix it.

The way I was hoping it would work is that since i = 0 and j = 1, the first comparison would be between ‘The’ and ‘quick’. If str[i].length <= str[j].length, then we remove the first element in the array using shift(), which in this case is ‘the’. That would make sense since ‘the’ is shorter than ‘quick’. If I flip the signs I would be removing ‘quick’ instead of ‘the’ which is the opposite of what I want.

Of course it’s not working as intended anyway now, but that’s my logic.

There are many problems on your code. I am not good at explaining but here is the code. Btw I’m new here.

let regex = /[a-z]+/gi;

function findLongestWordLength(str) {
str = (str.match(regex));
while (str.length != 1){
    if (str[0].length <= str[1].length){
    str.shift();
    } else {
      str.splice(1,1);
      
    }
}
  return str;
}

findLongestWordLength("The quick brown fox jumped over the lazy dog");

Thanks for the tip on split(). Did we cover this in a challenge that I forgot about? I’ve been trying to mostly avoid looking up functions to use for specific tasks, but maybe I should be doing more of that.

In terms of your question, I would probably write down the word and the page it’s on (maybe the number of letters too if it’s really long).

This is good stuff. I’ve struggled with how to deal with for loops when taking items out of the array, because it seems to cause problems with the variable counters (not sure if that’s what’s happening here). But it makes sense that if I’m always getting rid of either the 1st or 2nd number in an array, then I can just keep comparing the first and second numbers in the array.

I was unsure what the use case of while loops was, but it a case like this where we are repeatedly changing the length of the array is one.

split's not covered in the lessons, but you’re allowed to solve these problems however you want. FCC encourages “read-search-ask” method, so searching for a new method is completely valid.

Right, you’d write down the longest string, or its length. And then you’d continue going through every word and compare to what you’ve written down. What would you do when you find a longer word?

I would cross out the old word and write the new one down.

Right! So, in code that writing down the longest length could be a variable assignment, and the crossing out and writing a new one could be reassignment.

let longestLength = 0; // assignment

longestLength = someString.length // reassignment

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