Your second "for" loop:


#1


https://www.codecademy.com/courses/javascript-beginner-en-XEDZA/0/5?curriculum_id=506324b3a7dffd00020bf661#


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


/*jshint multistr:true */

var text = ("hello, my name is ryan and it is the best name ever!");
var myName = ("ryan");
var hits = [];

for (var i = 0; i <= text.length; i++){
    if ( text[i] === myName[0]){
        for ( var j = i; j < i + myName.length ; j++);
    }
    newArray = [];
    newArray.push('ryan');
    newArray[0]; 
}


#2

your first for loop:

for (var i = 0; i <= text.length; i++){
    // loop content
}

your second for loop:

for ( var j = i; j < i + myName.length ; j++);

why do you have no curly brackets ({}) for the loop content of your second loop?

As content of the second for loop, push the correct letter into your hits array


#3

i need some explaination on why the condition for the 2nd loop is i + myName.length and what must we push in to the hits array
myName[j] or text[j].


#4

please read these topics:


i wrote extensive explanations in them, seems you can use them


#5

i still dont get the part why must the 2nd loops condition be j< myName.length + i;


#6

because your name is somewhere in your text string, thanks to your if condition i contains the first letter of your name in text, so we need to stop our second for loop somehow, we can achieve this by using start value + myName.length which which will give us the stop value


#7

Thats was really helpful dude..mucha gracias tim


#8

Thanks for the help. I am now stuck here:

var text = ("hello, my name is ryan and it is the best name ever!");
var myName = ("ryan");
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(j);
}
}
}
if (hits.length === 0){
console.log("Your name wasn't found!")}
else
console.log(hits);

The exercise says I'm good to go and can start the next lesson, however my console is printing out: [22, 54]

Thanks


#9

unnecessary semi-colon after j++);, this causes problems.

and you push j into the array, but j is just an index. j does match your indexes of your name found in text, so you should use j as index to access value from text string


#10

Sweet! Thanks for the help!

-R


#11