Can not pass quiz ))

Hello everyone ) I can not pass the quiz : https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/find-the-longest-word-in-a-string
Who can explain me what is wrong with my code ?)

function findLongestWordLength(str) {
   var res = str.split(" ");
    console.log(res);
let nr = res.length;
    console.log(nr);
for (let i = 0; i < res.length ; i++ ){
  let j = 0;
  j++;
  if ( res[i].length > res[j].length  ){
   console.log(res[i].length);
   
  return res[i].length;
  }
}

}

findLongestWordLength("What if we try a super-long word such as otorhinolaryngology");

So, inside the loop, you are taking res[1].length and comparing it to res[2].length. The first time the first word is longer than the second, you’re returning a value. But what happens in this string:

Bob is SMART

First pass, the first word is longer than the second word, and it returns. But the THIRD word is longer than both, and has been ignored.

This is the first of two problems I see. Start with that, see where you get.

I dont understand what I must do with code(

Blockquote First pass, the first word is longer than the second word, and it returns. But the THIRD word is longer than both, and has been ignored

But I have passed first four conditions

When I start getting bogged down, I will often try to write it out as a series of steps, in perfectly plain and understandable language. Sort of a lazy ‘pseudocode’. Try this on:

  • Split the string into words.
  • create a variable to hold the longest word, start it with the first word.
  • start looping from the second word, to the end of the words array.
    • check if the current loop word is longer than our stored word.
      • if it is, replace the contents of our longest word with this one.
      • if it is NOT, simply continue the loop.
  • end the loop.
  • return the contents, or the length, of that longest word variable.

That’s kind of what I do when I get hung up. I break it down to tiny steps, each of which could be a line or two of code.

I just want to know why in some cases my code is counting wrong, and in the others counting good

Because, if the first word isn’t longer than the second word (j will always = 1, with your given code), the it’ll see if the second word is longer than the second word, then the third word is longer than the second word… until a word is longer than the second word.

I’m not surprised your code failed a couple tests, I’m more surprised it passed as many as it did. Fluke of the string they passed in, really.

with the string the quick brown fox jumped over the lazy dog, you’re res[j] will always be quick. The first word is shorter, the second word is the same, the third word is the same… all the way till res[i] is pointing to jumped, at which point it passes. As I said, it’s largely a fluke that your code passed, the second word just happens to be the second-longest word in the sentence.

And what to do with code ?Add new condition with var j ?

Do you see my pseudocode above? Does that make sense?

Kind of breaks it down. You only need one loop variable, i is perfectly acceptable. Create a variable and stick the first word in it. Start looping from the SECOND word, and compare it to your stored word. Do this for EACH member of the word array and, if it’s longer than the stored word, replace the stored word. Boom. Done.

There is an easier way, though, but it would take a little searching and messing with Array.sort() to get there. (Yes, that was a broad hint)

function findLongestWordLength(str) {
     var res = str.split(" ");
    console.log(res);
let nr = res[0];
console.log(nr);
for (let i = 1; i < res.length ; i++ ){
    if (res[i].length > nr.length){
        nr = res[i];
            console.log(nr.length);

    } else { nr = 1; console.log(nr);}

}
  
  

}

findLongestWordLength("Google do a barrel roll");

code is working ) but not pass(

Very close indeed!

So you log the nr.length – but do you return it? After all the looping is done, return that value.

function findLongestWordLength(str) {
     var res = str.split(" ");
    console.log(res);
let nr = res[0];
console.log(nr);
for (let i = 1; i < res.length ; i++ ){
    if (res[i].length > nr.length){
        nr = res[i];
            console.log(nr.length);

    } else { nr = res[0].length; console.log(nr);}

} return nr.length;
  
  

}

findLongestWordLength("What if we try a super-long word such as otorhinolaryngology");

not passed …

After i’d said to just return nr.length, you went and added that else loop. Which broke the whole thing again. Remove the else, doesn’t need to be there.

1 Like

I have passed ) thank you :slightly_smiling_face:

1 Like