Can someone explain to me how this second for loop works?


/*jshint multistr:true */
var text = "Bla bla bla test bla bla bla \
bla bla bla bla test bla test"

var myName = "test"
var hits = [];

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

if (hits.length > 0) {
} else {
console.log("Your name wasn't found!")

I don't understand the second for loop. First, why would j ever be > than text.length + i. Second, why is push(text[j]) and not just [j]. I just don't understand the whole second for loop in general :confused:

Your Second For Loop Help
Have some questions in regards to this code
Lesson 5
5. i dont know what to do
Your second for loop
Do I understand the second 'for loop' properly?
I am getting an **Out Of Memory** error and my code seems to be correct
Neem Help "Your second "for" loop"
5. your second for loop
Your second "for" loop
How am I using the .push() statement incorrectly?
How am I using the .push() statement incorrectly?
5. Your second for loop help needed
This nested for loop is causing me massive issues
Your second "for" loop explanation
Your second "for" loop:
Searching text for your name
I don't know how to set the second for loop
Second "For" loop confusion

let's do a simple text variable:

var text = "test bla"

agree with me that we can index a string:

t   e   s   t   _   b   l   a
0   1   2   3   4   5   6   7

i put in a underscore for the space. Now, we wrote a first for loop:

for (var i = 0; i < text.length; i++) { }

to loop over the characters, if you where to add a console.log statement:

var text = "test bla"
for (var i = 0; i < text.length; i++) { console.log(i) }

we would see the numbers 0 till 7 (7 included), now we could use these numbers to access the letters indexed at these positions in our text string:

var text = "test bla"
for (var i = 0; i < text.length; i++) { console.log(text[i]) }

Now, you use an if statement:

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

to see if there is a hit, agree that on our new text variable (var text = "test bla") i is 0 the moment a hit in encountered? So now the second for loop will run. We use j in our second for loop to push our hits. j will be set to 0 (since i is 0), then will run as long as it is smaller then i (0) + myName.length (4), so that will be: 0,1,2,3.

What a lucky day, that are the indexes our name is at :smiley: which then will be pushed into an array, so we can print them in the next exercise. Hope this helps

I passed this but I just need someone to explain the second "for" loop!

Thanks for that explanation!


I'm still not understanding the "run as long as it is smaller than i (0) + myName.length (4)"


Well, we use i to loop over the string we stored in text, that make sense right? So the moment a hit is encountered, i holds the index of the first letter of the hit/your name, still make sense?

So, now we want to from this index (in our example 0) till the end of your name (which is at 3)?:

t   e   s   t
0   1   2   3

So we can push it into an array, so we can print it later. What is the best way to achieve this? By using the length of your myName variable. The second for loop will run, till the last letter (t) is pushed into your hits array


Alright, I understand now. Thanks!