#5 second for loop


#1

Great Post!

/*jshint multistr:true */

*var text = "puke, power, top, Sean,bottom, bear, Mark,Todd,crap*
Holy,bear,top,Mark,Sean,Todd,Mike,Charles.";
var myName = "Sean";
var hits = [];
for (var i = 0; i
** if (text[i] === "Sean") {**
** for (var j = i; j< (i+myName.length);j++){**
** hits.push(text[i+j]);**
** }**


** }**


}

That's my code. I'm getting the Oops, try again. It looks like your second 'for' loop isn't pushing values to the hits array. Make sure it's working properly and that myName's text appears somewhere in the text variable.

error :(.

Replace this line with your code.


#2

Can you post a link to the exercise you're working through and paste an exact copy of your code?


#3

var text = "puke, power, top, Sean,bottom, bear, Mark,Todd,crap\
Holy,bear,top,Mark,Sean,Todd,Mike,Charles.";
var myName = "Sean";
var hits = [];
for (var i = 0; i<text.length; i++){
    if (text[i] === "Sean") {
        for (var j = i; j< (i+myName.length);j++){
            hits.push(text[i+j]);
        }
        
    }
    
}

#4

This is the snag. Your if condition never triggers the 2nd loop. The first loop is going through the text variable, character by character. It won't recognize a text string like "Sean". It would recognize any individual character from your text variable. What you should do is pick the character that will allow the 2nd loop to execute and collect characters that match up with your myName variable into the hits array.


#5

AHA! DOH! That makes total sense, otherwise, what's the point of the second string :).

Thanks!


#6

It took me weeks to understand and only by reading multiple explanations on the forum :smile:


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.