5 - Please explain the j = i and text[1] === "myName" etc


I'm at the section where there is a second loop and don't understand the concept of adding the second loop... using j =i, hits.push(), text[i] === myName etc.
Can someone please explain? Thanks

5. Your second "for" loop

Well the first for loop goes through your text letter by letter. Now what you do is to check whether the current letter is the first letter of your name. Because then the chances are good that the following word is your name that's why you check for

if(text[i] === myName[0]){...

Now if it is you want to add the next X letters to your array. As you suppose this to be your name X is myName.length. As you found the matching letter at position i in your text this is the point where you start and as you don't want to change the value of i but just want to have another loop you use a different variable:

var j=i;

The stopping conditions is as said i+myName.length. Current position + #letters in your name. Now the way you add them to your array is by using hits.push() which appends what you use inside the () to the end of the array on which you call it. Any questions left?


I get this result for that:
[ 'Ivan', 'Ivan', 'Ivan', 'Ivan', 'Ivan', 'Ivan', 'Ivan', 'Ivan', 'Ivan', 'Ivan' ]

Is that ok?


No it should contain only letters did you pushed myName[j] or just myName?
Anyway it would be better to post your question in a new thread and post your code as well so that one can see if there is a problem and where.


thanks for this explanation tbh i was finding it a bit confusing in the lesson