5. Your second "for" Loop - I AM SO STUCK


<Below this line, add a link to the EXACT exercise that you are stuck at.>

<In what way does your code behave incorrectly? Include ALL error messages.>

Every time I run this code, my browser crashes.


/*jshint multistr:true */

var text=“Hey my name is Jordan, I love cats, dogs, and did I mention my name is Jordan”

var myName=“Jordan”

var hits =

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



<do not remove the three backticks above>

There may be a good reason for that :slight_smile:

If you look at this portion of the code: [quote=“1337man, post:1, topic:35398”]
for (var j=i; j<i + myName.length; i++) {

The letter being incremented is the j, not the i.

If the i were to be incremented, the code would constantly run to satisfy the condition, the purpose there are for loops and other loops. Since it is a loop, there has to be a condition for the action inside the loop to be executed appropriately, and it will always be satisfied because the i will always increase more than the value of j, thus being an infinite loop.

And you know how complicated infinity can be…

1 Like

Fixed it! Thanks for the help and the explanation to go with it. Cheers mate!

`var text = “Hey my name is Jordan, I love cats, dogs, and did I mention my name is Jordan”;

var myName = ‘Jordan’;

var hits = ;

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

this worked, change the i++ to j++

What could I have done wrong?

Your code searched for “Amor Foods” in the text and didn’t find that’s why it gave you the Unexpected token ILLEGAL error!

when trying to find a word in a line of text, that word you’re trying to find must be in the text to start with.