New j variable in challenge 6. "search text for my name"


Don't understand why we have to create a new j variable in the challenge 6. "search text for my name"


In topic 6 in Search text for your name?
I think you mean j inside second for loop? If so, I can explain that, just confirm I'm on right track.


yes, the whole new if loop with j is confusing me alot.


UMMMMM Help me Idk what's wrong.

Console logs first letter of myName

This is that loop.

var text = "Hey, how are you doing? My name is Igor. Igor Igor. Igor is my name, yes";
var myName = "Igor";
var hits = [];

for(i = 0; i < text.length; i++) {
    if(text[i] === myName[0]) {
        for(j = i; j < (myName.length + i); j++) {

You understand what code will search for. So, lets start.
First for loop will start at text[0], first position, and go until comes to an end.
In every iteration, it will compare current position, text[i], with first string of your name, in my case with "I". If it finds that text[i] === myName[0], it goes into second while loop.
Second while loop will start at current "i" position (notice j = i), and use "j" as its counter. It will end when it reaches current position index + length of your name, in my case, lets say that "I" was found on position 10, and myName.length = 4, so it will run from 10th till 13th index (because we use j <, it will terminate one position before current index + name length).
It will just push number of characters in your name into variable hits and that's it. Now this wont work perfectly correctly, because if I had var text = "Ignore this is wrong, Igor"; inside hits instead of just hits = ['I', 'g', 'o', 'r'] we would have
hits = ['I', 'g', 'n', 'o', 'I', 'g', 'o', 'r'] (I didn't put this in code so I can bold this part that wouldn't be actual solution, but would be included).

Let me know if I can make solution simpler or you have more questions.


Thank you so much Igor, it makes so much more sense now. I really appreciate your time. I feel this wasn't explained well at all in the challenge. Thank you thank you.


No problem, glad I could have helped.