Stuck at the second loop


#1


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


So it is saying that I am not pushing the code, but I believe I have it correct. I don't have a very good understanding of for loops to begin with, so it may be incorrect.


/*jshint multistr:true */

var text = "The quick brown fox jumped over the James lazy dog."
var myName = "James"
var hits = []

for(i=0; i<text.length; i++){
    if(text[i]=== 'J'){
        for(j=i; j<(myName.length+i); j++){
            
            hits.push(j);
        }
    }
}


#2

Push an item from the string...

text[j]

#3

So I see that that works, but why couldn't it be text[i] if I said that j=i?


#4

Because i is static during the operation of loop. j is incrementing, so will access all five letters.


#5

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