Second For Loop Assistance!


#1



Currently, there are some syntax errors; I can fix those, I am just worried about the code itself.


/*jshint multistr:true */

var text = "Hey, Bob. Thanks for researching me about the position. \
Maybe we can grab coffee on Friday? This is Mike. Kind regards, Mike";
var myName = "Mike";
var hits = [];

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

This is my code, and as many people have, I am struggling on the second for loop. I get everything up until what I bolded (**), which is the second for loop. I am also wanting to understand how the computer is thinking. I do not even understand why a second for loop is necessary if we are already matching and looping with the first letter of name and what is inside the text. I get that we must push on letters to our hits array, but can that not be done within our first for loop and if statement?

Please help me understand the second for loop and what I did wrong. The i and j part is also a little confusing. And, the second condition of people talking about j+i myName.length. Not sure why we have that. And, yes - I understand hits.push(j);** is pushing a number.

Thank you.


#2

@netsurfer41940,
Try to read this
https://discuss.codecademy.com/t/i-passed-this-but-i-just-need-someone-to-explain-the-second-for-loop/30895/2?u=leonhard.wettengmx.n
maybe you will understand....


#3

`
//String
var myText = "Call Me John";

//Name Searching For
var myName = "Mel";

//This Splits the myText string into an array and stores it in the textArray variable [C][A][L][L] [space] [M][E] [space] [J][O][H][N]
var textArray = text.split();

//init the match array
var matches = [];

for(i=0 j=0; i < = text.length; i++){

//If a match, push to matches array and move onto the next letter in the string
if(textArray[i] == myName[j]){
     hits.push(textArray[i]);
      j++;
  }

}

/*You can also do the following by using the JS conditional Conditional (ternary) Operator*/
/*https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator*/

    for(i=0 j=0; i < = text.length; i++){

    //If a match, push to matches array and move onto the next letter in the string
     textArray[i] == myName[j]) ? hits.push(textArray[i]) , j++ : null   
  }

`