I've gotten lost with the introduction of the j variable here


Hey guys,

on the second for loop Ive gotten a bit lost with whats going on. Looking at other peoples code I gather that its supposed to look something like:

for (var j = i; j < (i + myName.length; j++) {

So I see that we've introduced a new variable j although I'm not sure why. Why introduce a new variable if it is just going to be equal to the old variable i?

The second part I dont understand is where the j<(i+my.Name.length) comes from. Why is it i+myName.length rather than just myName.length? In all the previous codes we never added the i variable before the my.Name.length.

Thanks I appreciate any help I can get on this!


Your example can be written,

for (var j = i; j < i + myName.length; j++) { // ... }

or at least add a ) like so, length);. As we can see, the parens are not needed. JavaScript interprets this correctly without them.

We need the second variable because this is an independent loop that might run several times (depending how often the first letter of myName comes up in text). The outer loop runs only once from start to finish. We need to initialize j to begin with i and advance from there. That is why we add the offset i to myName.length. If we didn't, the inner loop might not run at all.

Let's say myName is "Wee Gillis" and it is embedded five times in 1000 characters of text data (around 200 words). For example, let's put the first occurence at index 50, so i == 50.

var myName = "Wee Gillis";
// console.log(myName.length); // logs 10
for (var j = i; j < i + myName.length; j++) {

j will step through index 50 thru index 60, but only push index 50 thru index 59 to the hits array.

" . . . Wee Gillis ..."

text     hits
[50] 'W' [0]
[51] 'e' [1]
[52] 'e' [2]
[53] ' ' [3]
[54] 'G' [4]
[55] 'i' [5]
[56] 'l' [6]
[57] 'l' [7]
[58] 'i' [8]
[59] 's' [9]
[60] return to outer loop and increment `i`

5/7 What does this mean?

To the codecademy moderator: I don't understand the part where "We need to initialize j to begin with i and advance from there", I've read the sentence a couple times but I just cannot wrap my head around what you're trying to say. This part of Java coding is more difficult, I try to do my best to understand what each element means but sometimes the explanations are not enough to satisfy their meaning.


Awesome you are the best. Thank you!


Apart from the fact that what we are doing is brutally rudimentary, it has some ciritical lessons in it once we get our brains looking for them. To this rudimentary form we can introduce incremental imprrovements.

Consider if myName were to be Wee Willis. What would the program output then?

['W','e','e',' ','W','i','l','l','i','s','W','i','l','l','i','s',' ','l','o','r','e']

Both W's are matched, and the inner iteration repeats over ten characters, regardless of subsequent match. Only the first letter matters.

We make our program smarter by not iterating over used characters. We found a match at index 50 and captured 10 characters. They are all now used. So let's move the outer loop iterator, i up from the 50 it is currently to the 59 it should be.

    for (var j ... ) {
        // ...
    i = j - 1     # remember i is yet to increment

Now we no longer iterate over used characters. A small improvement, but a good one.


What if the last words are, "You Win!"? What will the program output?

['W', 'i', 'n', '!', undefined, undefined, undefined, undefined, undefined, undefined ]

If text ended with '!' then any index pointer beyond that is undefined, and JavaScript knows that and tells us so.


Here is my code with explanation. Took an hour for it to click what was going on in the second for loop:

var text = "hello Chris. Nice to meet you Chris. Are you learning programming Chris";

var myName = "Chris";

//empty variable where my name letters will be pushed
var hits = [];

// i starts at 0. as long as i is less then the length of characters it will loop. Add one to i. Will go through each character 1 - 56.
for (var i = 0; i < text.length; i++) {

//Check each character in text until it finds C. First C stops on 6 in text length.
if ( text[i] === "C") {

// j = i or the 6th character from first loop. Setup is j = 6. Condition is 6 is less than myname length or 5 + i which is 6 = 11. Add one after the loop. push j or 7 8 9 10 in the text to var hits. Stop sat 11. and places no more characters.
    for (var j = i; j < (myName.length + i); j++) { 




for (var j = i; j < (myName.length + i); j++) {


Ill try and explain it a little better: Remember when you loop the string of text its counting the characters. It's not an array which starts 0, 1, 2, 3... It's counting the text characters in the string which is 1 , 2, 3, 4, 5, 6... until it gets to the last character. So the first loop says i is 0. As long as i is less than the entire count of all my characters then keep looping.

Your if statement says, check each character during the loop and if any = C run the next code block or for loop.

Ok so we found C. In my example my name C was the 6th character in my text string. So what we want is to push C and the rest of the letters of your name. So what it's doing, it's finding C, then checking how many characters long your name is in var myName, in my case Chris is 5 characters long. It's not actually checking for h, r, i, s. It just wants to find C then know to push C and the 4 characters after it to the array.

so the code breaks down like this. In my first loop i finds C as the 6th character. so technically at the moment i = 6. We create another for loop and make it j = i. We want to stay at the C character we found or 6th character so we start j off to what i is or 6. j = i = 6. So our second for loop is going to start at the 6th character spot or C. That is the setup. j = 6 or where we found the C. Condition part is how do we make it loop and know where to stop.

j < (myName.length + i); Remember order of operations. myName.legnth for Chris is 5. i is currently 6 where my first loop stopped to find the first C. j = i so j is actually 6 as well. So: As long as 6 is less than 11, keep looping and add one. Then push characters 6, in the first loop, character 7 in the next. Remember it's adding 1 to j each time. j starts at 6. 7, 8, 9, 10... until j is no longer less than 11 and it stops the loop. Then it runs the outter loop again until it finds your name again and continues the loop all over.

I hope this makes sense.

Let me know if there is a part you didn't understand and i'll try to clarify it.


Hi, cool explaination! Thanks for being very detailed, helps us newbies a lot!

Thank you,