How does this work for loop and if work?

Trying to figure out in my own understanding how the for loop works and if the statement of that function works. This is because when faced with the problem, this code is shorter, but the same result as my original one. The confusion is on the longest variable. It stores the longest word lengths larger than str.length (5), or I could be wrong. For some obscure reason, the length of language (8) is not stored in a variable, although 5, 10, and 18 are.

function longestWord(str) {
    str = str.split(" ");
    var longest = 0;
    var word = null;
    for (var i = 0; i < str.length; i++) {
        if (longest < str[i].length) {
            console.log("str = " + str[i]);
            longest = str[i].length;
            console.log("longest = " + longest); //What happended to 8 for language?
            word = str[i];
        }
    }
    return word;
}
console.log(longestWord("Using the JavaScript language bademnostalgiastic"));

      

+3


source to share


5 answers


JavaScript

is 10 characters long and is tested before language

, so the test if

fails and is skipped.

It stores the longest word lengths greater than str.length (5)



Not. It retains the longest length still visible. It gets 5

when Using

checked, but this is quickly overwritten.

+1


source


All of this keeps track of the longest word (and keeps a char count in longest

). For each iteration, it checks if the next line has more characters than the longest line currently written (determined longest

). If so, it keeps a new char count, as it is the new "winner of longest".

Here's what's going on here:



  • take a string and split it into words (defined by spaces)
  • at this point you have a string array of all individual lines separated by ""
  • loop through all the lines in the array
  • If the current line you are iterating over has a more character count than any other previous ones, then store that current character count in a variable longest

  • continue the loop and use the above logic at the previous point

So at the end of it all, you have the actual string (stored in word

) and the number of characters (stored in longest

) the word with the most characters.

+1


source


The length of "JavaScript" is 10, which is longer than "language". Since "JavaScript" comes first, "language" will not be longer than the longest, so the if statement will result in false.

+1


source


array str[]={Using, the,JavaScript, language ,bademnostalgiastic}

Iteration 1

str[i]=Using

str.length=5 (a)

Longest =0   (b)

since (a)>(b)

Longest =5

word=Using

      

Iteration 2

str[i]=the

str.length=3 (a)

Longest =5   (b)

since (a)<(b)

Longest and word remain same

so,Longest =5

and,word=Using

      

Iteration 3

str[i]=JavaScript

str.length=10 (a)

Longest =5   (b)

since (a)>(b)



so,Longest =10

and,word=JavaScript

      

Iteration4

str[i]=language

str.length=8 (a)

Longest =10   (b)

since (a)<(b)

      

remains the same for so long

so,Longest =10

and,word=JavaScript

      

Iteration5

str[i]=bademnostalgiastic

str.length=18 (a)

Longest =10   (b)

since (a)>(b)



so,Longest =18

and,word=bademnostalgiastic

END OF LOOP

      

such a long word bademnostalgiastic

+1


source


Here's the breakdown:

str = str.split(" ");

This makes an array of strings separated by spaces.

for (var i = 0; i < str.length; i++)

We start here with i

(iterator variable) at 0. We will continue to make this loop, and i

less length str

. We will be incrementing i

by 1 each time we go through this cycle.

if (longest < str[i].length)

Here we check if it is saved longest

which is less than the length of the line we are looking at.

longest = str[i].length;

Here we are assigning a new long string because it is longer.

word = str[i];

We'll also save the word, probably so we can use it later.

return word;

After the end of the loop, we will send word

back as the result.

console.log(longestWord("Using the JavaScript language bademnostalgiastic"));

This is your call and print operator.

The reason you see 5, 10, and 18 is because you only print values ​​when the value is greater than what you have already seen.

0


source







All Articles