Javascript doubt in "Your second for loop"


#1

Okay so this is the part where you make a variable with lot words in it, and your name is somewhere in between it. And then, you create an array ( Did i?) and then you make a loop so that it supposedly keeps looping until it finds my name. But so far I dont seem to get whats going in in my code, and whats the error here. I feel suddenly overwhelmed, so help me :smile:

/*jshint multistr:true */
var text = "Bloh bleh blah blah blih neelson blah blah bluh bluh bluh blah" ;
var myName = "neelson" ;
hits =[] ;
for (i=0 ; i<=text.length; i++) {
    if(text[i]==='n') {    
        
        
         for(n= i;n<=myName.length;n++){
             newArray = [] ;
             newArray.push('hello') ;
             newArray[0] ;
             
             }
        }
    
    }

#2
var text = "Bloh bleh neelson blah blah bluh bluh bluh blah" ;
var myName = "neelson" ;
var nameLength = myName.length;
hits =[] ;

//Loop from character-0 upontill text.length character for character
for (i=0 ; i<text.length; i++) {
    //check if character text[i] equals first-character of myName
    // could also use text[i] === myName[0]
    if(text[i]==='n') {    
        // Bloh bleh neelson blah blah
        //           ^      ^
        //           |      |
        //      text[i]     |
        //           |     i + myName.length
        //           V
        //from     n=i  until n < i+myName.length

         for(var n= i; n < i+myName.length;n++){
             hits.push[ text[n] );
         }
    }
  }
  console.log( hits );

References:

google search
javascript push array site:developer.mozilla.org
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push

javascript push array site:stackoverflow.com
= http://stackoverflow.com/questions/351409/appending-to-array

www.crockford.com
http://javascript.crockford.com/survey.html
http://javascript.crockford.com/code.html


#3

var text = "Blah blah blah blah blah blah Sara \
blah blah blah Sara blah blah Sara blah blah \
blah blah blah blah blah Sara";
var myName = "Sara";
var hits = [];
for(var i = 0; i < text.length; i++){
if(text[i] ==="S");
for(var j = i; j<(myName.length + i); j++){
hits.push(text[j]);
}
}


#4

I didn't understand any of that. But this will run.


#5

lmao it's one of those annoying moments where you wonder "Why does my code work?" xD


#6

Its nested loop.
I so hate nested loops...
They are so confusing.


#7

Thanks a lot sir...it solved it for me.Thanks for the explanations too!