Need help understanding why j = i, the second "for" loop



I'm starting to understand this problem but for some reason I cant wrap my head around the j = i and how the computer sees it. I've read some other posts but I don't know why I find this so difficult to comprehend. So the question is...

why does j = i? In my head if j = i, why not just use i?

what does the computer see when i have

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

heres the whole code:

var text = "Shabby Matt chic pug biodiesel flexitarian next level locavore church-key, messenger bag brunch occupy. Keffiyeh Matt green juice chicharrones, gochujang vice salvia kickstarter dreamcatcher mlkshk shoreditch Matt disrupt ramps. Disrupt poutine helvetica, neutra blue bottle offal tote bag wayfarers.";

var myName = "Matt";

var hits = [];

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



because i is looping over your text. j is used to push the letters of your name into the hits array. So i contains the index of the first letter of your name (thanks to your if condition)

If you where to use i, how are you possible going to tell when your name ends? Because i doesn't change as the second for loop is running, you can use i+myName.length to tell when your name ends


So the way Im starting to see it is, j = i(which is the position of the first letter of my name) this tells the program that "j starts here!" then the computer pushes the next however many positions of the letters of text to "hits". It knows to stop because I told it i+myName.length. Which I believe translates to (every position till now) + (length of myName)

Am I close?


close? You can't get any closer, you got it :slight_smile: