5: Your Second For Loop


#1


I already completed everthing, but I do not understand the second for loop where j < (i + myName.length)


/*jshint multistr:true */
var text= "Temmie is love, Temmie is life, awesomeness, the epitome of it is the man, the myth, the legends, Temmie";
var myName= "Temmie";
var hits= [];

for(i=0; i < text.length; i++){
    if(text[i] === "T" ){// cant figure out the below
        for(var j = i; j < (i + myName.length); j++){
           hits.push(text[j]); 
        }
    }
}

if(hits.length === 0){
    console.log("Your name wasn't found!")
}
else{
    console.log(hits);
}


Replace this line with your code.


Help would be greatly appreciated,
Thanks so much!


#2

In the above, j is the inner loop iterator variable. It is set to i, so it follows we need that value as an offset, so i + myName.length.

Take the second occurence of your name. It is at index 16 and beyond. i will be 16

16 + 6 = 22

So this will look like,

for (var j = 16; j < 22; j++)

T  e  m  m  i  e
16 17 18 19 20 21

#3

That helped a lot! Thanks for your help!


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.