The second loop explanation


Hi all. I finished this exercise, but I does not understand how the second loop works.
Please could someone to explain me? Why we type " j < (myName.length + i) " ?

Thanks in advance!


the loop looks like this if i remember correctly:

for (j = i; j < (myName.length + i); j++)

in your first loop, you use i to loop over you text and to find your name. Let's say it found your name when i is 10, and your name is 4 characters long. now j will be 10, and loop over your name (10, 11, 12 and 13) and push this in an array. In this time, i will not increase, so you need the length of your name + i to ensure j pushes the right letters into the array


Can you explain this in a simpler way please? I'm not sure that I'm understanding your explanation correctly.


Is there anything specific you do not understand? Well, the simple explanation would be you use your first for loop:

for (i = 0; i < text.length; i++)

the moment your for loop encounters a hit:

if (text[i] == "M")

i has a number (10 for example), the index number of your hit, you set j equal to 10, then you let j increase until it reaches the last letter of your name. i + myName.length indicates what the last letter of your name/hit is