5. Your Second For Loop - Question


#1


https://www.codecademy.com/courses/javascript-beginner-en-XEDZA/0/5?curriculum_id=506324b3a7dffd00020bf661#


Code works. I have a question on the middle part of the 2nd for loop.
for(var j = i; j < myName.length + i; j++) {

var j = i --> in the previous for loop we established that the "K" is the first letter and that its position is 0. Then in second part we say j is less than myName.length + i. But if i = 0 there's no reason to include the +i in the second part of the for loop.

I feel like i is probably equal to 1 now and that's why we have to include it on the second part. Can someone shed some light on how this works? I know I'm missing it!


/*jshint multistr:true */
var text = "bla bbla bla blba bll hi, how are you blba bla bla bla bla bla Kevin blab bla hi ahow bla bla bla bla bla";
var myName = "Kevin";
var hits = []
for (var i = 0; i < text.length; i++) {
    if(text[i] === "K") {
        for(var j = i; j < myName.length + i; j++) {
            hits.push(text[j]);
        };
    }
};


#2

yes, in myName K is the first letter, but you want to find the K in the string stored in text. K is there absolutely not the first letter. in fact, in text Kis at index 63. so we needi + myName.length to get your name


#3

Ahhhh, that's what I was missing. Thanks, stetim!


#4