Second for LOOP - I am so confused

I really don’t understand what’s wrong with my code. I’ve seen people add +i after the myname.length to the second for loop but I dont get why.

Thank you.

/*jshint multistr:true */
var text = “Hi my name is Ilsse and I am from Mexico, I like to play video
games did i tell you my name was Ilsse right, I did”;
var myName = “Ilsse”;
var hits = ;

for (i=0; i < text.length; i++)
{
if (text[i] === “I”)
{
for (j=i; j > myName.length; j++)
{
hits.push([j]);

}

}

:bug: Alert!!! Dont see problems in your code.

Weird @amanuel2 is the problem broken?

Im Afraid not,hope codecademy fixes it soon. What exercise is this so i can check on mine?

It is Unit 3 , exercise 5 called : Your second “for” loop

Try This if it works

    /*jshint multistr:true */

text=" what what what what what what what what what what what/   what Eric"
    var myName= "Eric"
    var hits = [];
        for(var a=0 ; a<text.length ; a++)
            if(text[a]=== myName[0]){
        for(var b=a ; b<(a + myName.length) ; b++){
             hits.push(text[b]);
        }
            }

Worked for me…

It did not work for me:

var text = “Hi my name is Ilsse and I am from Mexico, I like to play video
games did i tell you my name was Ilsse right, I did”;
var myName = “Ilsse”;
var hits = ;

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

}

}

I did what you did and I am still getting :
Oops, try again. There was a problem with your syntax.

Why did you add the (a + myName.length) section?

I dont know lol sorry. Completed the skill 2 years ago

1 Like

@chiprunner59720 take a look a look at your code i have commented why you had the error and added some to it

var text = "Hi my name is Ilsse and I am from Mexico, I like to play video \
games did i tell you my name was Ilsse right, I did";
var myName = "Ilsse";
var hits = [];

for (var i=0; i < text.length; i++){
    //here in your condition
    // you can make it such that
    //if your name changes you dont have to change anything
    //by doing text[i] === myName[0]
    if (text[i] === myName[0]){
        
        //good idea to create a variable
        // to store all the subsequent substrings
        //from the second for loop
        var found = "";
        
        
        
        // here you had an infinite loop originally
        //because you wrote j >myName.length
        //and it  will be true an always will be true
        //when a match of my first initial is greater
        //than the length of myName
        for (var j=i; j <myName.length+i; j++){
            
            //we are not going to push to hits yet
            //hits.push(text[j]);
            
            //as we iterate the text we are capturing
            //all the characters and adding it to found
            found+=text[j];
            

        }//end second for-loop
        
        //debugging statement
        console.log(found);
        
        
        // each time the second for loop is done the compiler
        //comes down here and will check if the string captured in 
        //found so far matches myName entirely
        //if it does then add it to hits
        //else do nothing with it
        if (found===myName) {
            //now we push to hits
            hits.push(found);
            
        }
        
    }//end if
}//you forgot to end the first for-loop here


console.log(hits);



you can test it here
#Click here to labs

4 Likes

the reason why he did a+myName.length is explained below



/*      that loop begins when the current character
        matches the first letter in myName. the loop will
        start at the index where you got the match(i.e.a) and will
        only run for the length of myName.
        
        Now we write this statement j<myName.length+a adding the
        'a' to ensure that we always begin the second for loop at the 
        index where we found the match
        
        */

2 Likes

Thank you sooooo much for your help!! You are awesome!! I definitely needed all that detail :grin:

Lol no problem! I really appreciate all the help! :grin:

Need help !
please tell me what is exactly equal to j and i .
thank you ! :grinning: