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


#1



https://www.codecademy.com/courses/javascript-beginner-en-XEDZA/0/5?curriculum_id=506324b3a7dffd00020bf661#

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++) {
            hits.push(text[j])
        }
        
    }
    
}


#2

There may be a good reason for that :slight_smile:

If you look at this portion of the code:

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...


#3

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


#4

`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++) {
hits.push(text[j]);
}
}
}


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



#5

What could I have done wrong?


#6

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.