Find the Longest Word in a String : pb with for loop

https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/find-the-longest-word-in-a-string
This is my solution I try to fix:

let result = 0;
  console.log(tabWord)
  console.log(tabWord.length)
  for (let i = 0; i < tabWord.length; i++) {
  tabletters.push(tabWord[i].length);
  }
  console.log(tabletters);
  for (let j = 0; j < tabletters.length; j++) {
    if (tabletters[j] > result) {
      result = tabletters[j];
      console.log(tabletters[j]);
    } 
  }

  console.log(result);
}

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

result = 7, that means the length of the first length of tabletters. The loop For with “j” doesn’t works.
Why ?

Can you give us all your code? You are missing your function declaration and it is not clear to me what some of those variables are.

Here:

  for (let i = 0; i < tabWord.length; i++) {
    tabletters.push(tabWord[i].length);
  }

If tabWord is a word, then you are just indexing through the letters in it. The letters always have a length of 1 so I’m not sure what you think tab letters is doing.

I think you are not trying a good approach. As an exercise, can you explain in pseudocode what you are doing? If you were explaining to a child how to do this, what would you say?

Sorry,

function findLongestWordLength(str) {
  let tabWord = str.split(' ');
  let tabletters = [];
  let result = 0;
  console.log(tabWord)
  console.log(tabWord.length)
  for (let i = 0; i < tabWord.length; i++) {
  tabletters.push(tabWord[i].length);
  }
  console.log(tabletters);
  for (let j = 0; j < tabletters.length; j++) {
    if (tabletters[j] > result) {
      result = tabletters[j];
      console.log(tabletters[j]);
    }
  }

  console.log(result);
}

findLongestWordLength("Theeeee quick brown fox jumped over the lazy dogggggg");

I found the solution with Math.max() surfing in internet. I had forbiden this methode.

In fact I wanted to check all numbers of the table and in case of j > “result”, to put it in the “result” variable but the problem is when the value is updated once, the program go out of the loop “for” and it’s normal.
Thus I would like to know for my personal knowledgements if you have a solution in order to continue to itérate the others numbers.

I don’t know what you mean by “forbidden”.

But OK, you have a working solution - at least it will if you return result from the function. I would describe it as:

Break the sentence into words.
Save the length of each of those words in and array.
Find the largest number in the array.
Return that number. <--- Missing this step.

In fact I wanted to check all numbers of the table and in case of j > “result”, to put it in the “result” variable but the problem is when the value is updated once, the program go out of the loop “for” and it’s normal.

I don’t understand. The for loop is working normally for me.

If I run this code:

function findLongestWordLength(str) {
  let tabWord = str.split(' ');
  let tabletters = [];
  let result = 0;

  for (let i = 0; i < tabWord.length; i++) {
    console.log('i =', i, ' --- tabword[i] = ', tabWord[i])
    tabletters.push(tabWord[i].length);
  }
  console.log(tabletters);
  for (let j = 0; j < tabletters.length; j++) {
    console.log('j =', j, ' --- tabletters[j] = ', tabletters[j])
    if (tabletters[j] > result) {
      console.log('It\'s bigger, making the swap!')
      result = tabletters[j];
    }
  }

  console.log('result = ', result);
}

findLongestWordLength("Theeeee quick brown fox jumped over the lazy dogggggg");

the output is:

i = 0  --- tabword[i] =  Theeeee
i = 1  --- tabword[i] =  quick
i = 2  --- tabword[i] =  brown
i = 3  --- tabword[i] =  fox
i = 4  --- tabword[i] =  jumped
i = 5  --- tabword[i] =  over
i = 6  --- tabword[i] =  the
i = 7  --- tabword[i] =  lazy
i = 8  --- tabword[i] =  dogggggg
(9) [7, 5, 5, 3, 6, 4, 3, 4, 8]
j = 0  --- tabletters[j] =  7
It's bigger, making the swap!
j = 1  --- tabletters[j] =  5
j = 2  --- tabletters[j] =  5
j = 3  --- tabletters[j] =  3
j = 4  --- tabletters[j] =  6
j = 5  --- tabletters[j] =  4
j = 6  --- tabletters[j] =  3
j = 7  --- tabletters[j] =  4
j = 8  --- tabletters[j] =  8
It's bigger, making the swap!
result =  8

That looks like it is working to me.

Yes :grinning: , That works !!
I was not scroll down enough in the console window and also there was too many “console.log”, it had become too confused.
I proud of me.

I wanted to say “forgotten” :upside_down_face:
Thank you Kevin!

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