Second for LOOP - I am so confused


#1

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]);

}

}


#2

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


#3

Weird @amanuel2 is the problem broken?


#4

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


#5

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


#6

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


#7

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?


#8

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


#9

@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


Your "for" second loop
5/12 Your second "for" loop
Code works but didn't understand why, please help!
Need some clarification / understanding on 1.5 Your second for loop
6/7 Log It! Unexpected Else Statement
#10

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
        
        */

5/7 I don't understand
#11

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


#12

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


#13

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


Problem?