Stuck on second for loop


5.Your second "for" loop

Oops, try again. It looks like your second 'for' loop isn't pushing values to the hits array. Make sure it's working properly and that myName's text appears somewhere in the text variable.

/*jshint multistr:true */
var text = "this is a long story Billel, one that you will find \
long and boring Billel, Billel do you undrstand?";
var myName = "Billel";
var hits = [];
for (var i=0; i>text.length; i++)
if (text[i] ==="B"){
for (var j="B"; j>myName.length+i; j++){

Hi all,

i'm stuck on this page, can't figure out what is wrong with the code,

Any help ?



You need to check loop's condition (both of the loops).
Your loops never runs cus of condition.

Check the initial value of j while you define it .
It should not be "B" ?


Hi @billelr,

I notice 3 errors, I will point it out for you to try figure it. See text in Bold

for (var i=0; i > text.length; i++)

Refer back to the for loop exercise, beware of creating infinite loop, take note of the > or < sign

for (var j="B"; j>myName.length+i; j++)

Two errors above, the second one being the same, take note of the > or < sign.

The first one, why j="B"??

The lesson offers some explanation as below (what I copy):

For your second for loop, keep the following in mind:

First, you'll want to set your second loop's iterator to start at the first one, so it picks up where that one left off. If your first loop starts with

for(var i = 0; // rest of loop setup

your second should be something like

for(var j = i; // rest of loop setup

Second, think hard about when your loop should stop. Check the Hint if you get stuck!

Hope you manage to figure it out. If you still have doubts, come back here with your codes. Happy coding, cheers! :slight_smile:

Edit: just noticed @greentreemee posted but yeah.


Fixed! Thank you ! :smiley:


@billelr, you're welcome. If you need more understanding, do search in the forum under this category, there are a few detailed explanations offered by others on why the code is written that way.

Cheers :slight_smile:


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