Your second "for" loop 5/7, need help understanding


Thanks in advance for stopping by,
I can't make head or tail of the second for loop,
I've managed to piece it together, and it works... but I still need to understand it...

var text = "Hey, how are you doing? \
Your name is Emily. \
No my name is Saad . \
Hi Emily! \
Did i mention my name is Saad ? \
Hi Emily! \
I know I must be sounding crazy but my name is Saad ! \
Emily calm down.";

var myName = "Saad";
var hits=[];

for(var i = 0; i < text.length; i++){               //[1] "i" is set 0, and then goes through text.
    if(myName[0] === text[i]){                      //[2] if "0" in myName( which is "S") equals to "i" in text
        for(var j = i; j < myName.length + i; j++){ //[3] Don't get this.. :\ 
            hits.push(text[i]);                     //[4] Something gets pushed somewhere.

Thanks again!

if(myName[0] === text[i]){ //[2] if "0" in myName( which is "S") equals to "i" in text

should actually read,

if the letter at index [0] of `myName` is the same as the letter at index [i] of `text`

[3] Here we need to consider that myName is some fixed length, shorter than text.length, and it may appear in the text at multiple index positions. The outer loop zeros in on those indices, each in turn as the outer loop progresses through iteration.

We initialize the inner loop to begin iterating at the index match position. The last letter will be at an index i + myName.length minus 1. We need to include i as an offset to be able to reach that point in text.

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

In your sample text above, "Saad", or rather "S" is found at index [58].

... S a a d ...
    5 5 6 6 6
    8 9 0 1 2
         58 + 4

j will iterate through and push the letters at index[58] thru index[61] to the hits array.

5/7 Syntax Error