I have been stuck on the second loop for so long and idk whats wrong


#1

/*jshint multistr:true */
var text = "bruh my name is Andrew Andrew Andrew";
var myName = "Andrew";
var hits = [];
for (var i = 0; i < text.length; i++)
{
if (text[i] === "A")
{
for(var j = i; i < (myName.length + i); j++)
{
hits.push(text[j]);
}
}
}


#2

your loop condition:

i < (myName.length + i);

how will this cause your loop over instance of Andrew in text string?


#3

I'm sorry I don't understand what you mean by that.


#4

i am asking you to explain the condition of your second loop, so i can understand where you thinking is going wrong


#5

so for(var j = i; i < myName.length + i; j++) should pick up from "A" which has been replaced withand will search for letters subsequently 1 at a time. I've seen other people do the same thing in the forum and none of the answers are helping me.


#6

yes, but look at:

 i < myName.length + i

how can you use two times i here, so i has to be smaller then myName.length + i

you should j here at the right place, so your loop ends so you can loop from j till end of your name (myName.length + i)


#7

ok thank you so much I didn't even realize I did that.


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.