Second For Loop (Question #5)


#1

Can someone please tell me what is wrong with my code? Thanks!

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


#2

Hey @monroy_george,

It may be a problem in a different place, for I see no problem in the code you are showing. Showing the full code would be helpful. :blush:


#3

var text = "Hello George, do you like the name George?";
var myName = "George";
var hits = [];
for (var i = 0; i < text.length; i++) {
if (text[0] === 'G') {
for (var j = i; j < i + myName.length; j++) {
hits.push(text[j]);
}
}
}

This is my full code.


#4

Got it. :smiley:

One problem in your code there.

If your error code is:

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.

Then the problem is in that part. You see, you used i, so you need to use i in your if statement, or your second for loop wouldn't able to do anything!

Correct statement:

if(text[i] === 'G'){


#5

That did the trick. Thank you so much!!