Second 'for' loop isn't pushing values to the hits array


#1

/*jshint multistr:true */
var text = ("i'm hungry")
var myName = "Julia";
var hits = []
for(var i = 0; i < text.length; i++) {
if (text[i] === 'J'){
for(var j = i; j < myName.length + i; j++) {
hits.push(text[j]);
}
}
}

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.


#2

Hi @julia_166

You have to change your for loop in

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

But I completed your code (you're going to understand more with console.log) :

var text = ("i'm hungry chicken Julia hungry chicken Julia")
var myName = "Julia";
var hits = []

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

console.log(hits);

Hold on :wink:


#3

it worked thank you so much! :smile: