Do I understand the second 'for loop' properly?


#1

Search Text For Your Last Name - Your Second For Loop

I got the code right, but I want to make sure I understand the logic.

  1. Is the second for loop activated when the first letter in myName is found within text?

  2. Is the second for loop saying that the new variable, j, begins at the old variable, i ?

  3. According to the hint, "your loop should stop when it hits the value of the first iterator (say, i) plus the length of your myName variable." I understand the length of the myName variable, but why is the value of the first iterator added? Is it because we've already checked for i?

  4. Is the hits method pushing the character to the array hits anytime it finds a matched letter from myName?


var text = "His name is Nate. The Royals are Nate's favorite team."
var myName = "Nate"
var hits = []

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


#2

I would put it slightly different, but yes. When if condition evaluates to true the if statement gets executes (in this case, the statement is your second for loop)

yea, thanks to your if condition i contains the index of the of the first letter of your name, you can see this:

var text = "His name is Nate. The Royals are Nate's favorite team."
var myName = "Nate"
var hits = []

for (var i = 0; i < text.length; i++) {
    if (text[i] === "N") {
    console.log("index: " + i + ", letter at this index: " + text[i])
        for (var j = i; j < (i + myName.length); j++) {
            hits.push(text[j]);
        }
    }
}

so then here:

for (j = i

you copy the value stored in i to j, this can always be a bit tricky. but look:

i = 12
j = i
console.log(j) // will log 12

how that make sense.

point 3 is a bit tricky. lets write the first case in which your name is found:

for (j = 12; 12 < 12 + 4; j++)

okay, so the letters of your name are at index: 12, 13, 14 and 15. this is why you use i + myName.length to get the right indexes. just using myName.length would give the wrong indexes to find your name in text.

Hope this helps, otherwise here are two more explanations i wrote:

explanation 1
explanation 2

point 4, no, once the first letter is found, it assumes it is your name, so if you where to insert Nico into the text, this will also be found

feel free to expand the program


#3

Thorough answer! That really helped.

Okay...so, when

if (text[i] === "N")

evaluates to true, meaning a match between "N" and a data in the string text (like you said, at index 12), the if statement is then executed. I think I got a handle on that.

So, if i = 12, j assumes 12 in the second for loop to leave off where the i did in the if statement. So, we execute the code, "N" is pushed to the array via hits.push(text[j]) and then increment to the next letter in myName which is "a", and so on. Maybe a little messy of an explanation, but I think that's the general idea, yes?

I don't understand this. Why are we going to evaluate j as long as it is less than i + myName.length? If we used myName.length, would the evaluation started over at index 0?

Right. Once "N" is found within text, it assumes it is a match from myName. This means the if statement evaluates to true, so via the letter will then be pushed to the hits array via the push method by searching within text and finding variable j. Do I have the proper understanding?


#4

Ah. So, it does work. It just returns an empty array. Not sure why, though...


#5

i would say j gets assigned the value stored in i, but yes, in essence you are right.

yes, it is :slight_smile:

we use the second for loop (j) to start at your first letter of your name (thanks to the if condition) then we need to loop from the start of your name til the end of your name (with the second for loop)

So we have your first letter of your name (and the index this letter is at in text), but how do we tell the second for loop where the last letter of your name is? (or rather, the index of the last letter of your name)

we use i (which contains the index of the first letter) + myName.length to get to the last letter, so the loop knows where to end

that i need to see, so much you could have done


#6

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