5. Your second "for" loop


#1


error: 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.


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


#2

the idea of the second for loop is that it finds the first letter of your name (thanks to the if condition), then you store the first letter of your name in j (j = i)

then we use our second for loop to loop over our name (which is from j till i + myName.length), then we increase j so it find all letters in our name

then j contains the indexes of your name which is hidden in text, so then we can use text[j] to get the letters of your name


#3

I been trying and trying for like 15min now and from what I understood its suppost to be:
from: hits.push("a");
to: hits.push(text[j])

what else am I missing?


#4

this is a very good step, now your loop. Lets walk through the good bits:

we use our first for loop to loop over text

we use an if condition to find the first letter of your name (alex)

Now, your second for loop should loop from the first letter of your name (you do this right) to the last letter of your name, how do you think you can achieve this?


#5

i literally cant get it!
:for(var j = i; j < i.length + myName.length;){
hits.push(text[j])

I thought the j++ is the one going through all my letter, but youre telling me that i am missing it, im very confused


#6

no, you had i++, not j++, that was what i was telling you.

i will maintain the first letter of your name, so < i + myName.length will give you the last letter, i don't see why you should use i.length


#7

ah i see that, but i changd that too, but there is still the same error


#8

can i see an updated version of your code?


#9

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


#10

please apply my suggestion


#11

I Dont know how to thank you enough, so have a virtual hug


#12