5. Your second "for" loop - should stop when reaches current point in the string+myName.length


#1
/*jshint multistr:true */
var text = "stuff blah blah blah John Smith blah John SmitJohn SmithJ";
var myName = "John Smith";
var hits = [];

for (var i = 0; i < text.length; i++) {
    if (text[i]===myName[0])   {
        var x = true;
        for (j=1; j < myName.length && x === true; j++)   {
            if (text[i+j]!=myName[j])  {
                x = false;
            }
            else if (j === myName.length - 1) {
                hits.push(myName);
            }
        }
    }
}
console.log(hits);

here's my code it looks like it works as am getting the right output on the screen but is saying that "carefull: Your second for loop should stop when it reaches the current point in the string + myName.length."
but have done that with ** j < myName.length**


#2

@bluntinit

Try doing this;

/*jshint multistr:true */
text = "Blah blah blah blah blah blah John \
blah blah blah John blah blah John blah blah \
blah blah blah blah blah John";
var myName = "John";
var hits = [];
// Look for "J" in the text
for(var i = 0; i < text.length; i++) {
	if (text[i] === "J") {
		// If we find it, add characters up to
		// the length of my name to the array
		for(var j = i; j < (myName.length + i); j++) {
			hits.push(text[j]);
		}
	}
}
if (hits.length === 0) {
	console.log("Your name wasn't found!");
} else {
	console.log(hits);
}


Remove the if/else statement.


#3

thanks i had realised that i had misread the question and they didn't want you to make sure only the complete phrase match is pushed. although i am quite happy with my code as a solution for finding phrase matches