Your Second "For" Loop isn't working


#1



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


My code gives me the error message "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." I have been trying to fix this for a while, I have looked at several discussion pages, and nothing works.


/*jshint multistr:true */
var text = "Jonah Jonah Jonah I'm not even trying to Jonah make an actual sentence Jonah Jonah";

var myName = "Jonah";
var hits = [];

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


#2

I actually looked at this to help me.


#3

Help you with what?
And I don't really think this code would help. There's something wrong with it.


#4

Hi this line

for(var i = text[0]; i < text.length; i++)

you should change the var i = text[0] with just var i = 0 .. Because with the text[0] it set the variable i to the first letter of the variable text but you should put it equal to 0


#5

Thanks so much! It works fine now!


#6

@cubixiii

for ([initialization]; [condition]; [final-expression]) {
   statement
}

In the Initialization of the 1st FOR loop
you should use
var i = 0;
instead of you i = text[0] which corresponds to the first-letter of the text which will be a "J"