Stuck on Your second "for" loop


#1


Hello everyone,

I'm having a lot of trouble figuring out the second for loop and why it's not working. Here is my thought process when reading through the code. Please let me know if i'm spot on and where my thinking is off.

Thank you


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


My browser crashes or freezes. I'm assuming this means that my loop goes on forever and never ends.


var text = "Random string of text Richard containing Richard and did I say my name is Richard"

var myName = "Richard"
hits = [];

/*Declare variable i in loop = to zero.
When text.length is greater than i which is set to zero, keep incrementing by 1. Run the code inside the box

If call text variable and pass i === the letter 'R' then

Set variable j = i
So j = R
When R is less than the length of my name, keep incrementing. So length of my name is 7 characters. R is 1 character. 

R Ri Rich Richa Richar Richard (condition satisfied).
Push results to array called hits.
*/

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


#2

Think about the variable that you should be incrementing in this for loop. Considering your check is on j


#3

Wow I see the problem now. Thanks for the help :slight_smile:

It's crazy how often small things get missed or overlooked in coding.


#4

Haha, I once had to look through a 1000 line project to find what was breaking everything and it was cause I incremented the wrong variable in a nested for loop, it's sooo easy to do!


#5

2 posts were split to a new topic: Error on inner for loop


#6

2 posts were split to a new topic: Current point in string + myName.length Error


#7

2 posts were split to a new topic: Stuck on your second for loop - Solved


#9