Really stuck with this exercise


#1



Hi, please can you help me with this "for" loop? This is 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'

<
{ for (var i = 0; i < text.length; i++) if (text[i] === 'E') { for (var j = i; j < i + myName.length; j++)
console.log(j)
console.log(text[j])
}


Replace this line with your code.


#2

@artemis-darcy

Here

Needs curly brackets at the end of this line for the codes to execute in between it.

Remove these lines. You must add text variable indexed as j to hits array using push method like this:

hits.push(text[j])

#3

Ok I understand. Thank you!! :slight_smile:


#4

I'm still struggling.

var text = "I am Guy, nice to meet you"

var myName = "Guy"

var hits = [];

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

I can't understand what's wrong with my code. Can anyone help?


#5

@textrockstar22540

If you have a problem, please post it in a new topic with your question + error message + code


#6

@textrockstar22540,

I'd like to echo @miniapple8888's comment, and also to suggest you reexamine your for loop.

Remember, the syntax for a for loop is:

for (counter = [where it starts counting from]; counter < or > [where you want it to stop counting]; how much the counter should increment by) {}

Are you sure your loop follows this format?

frisby


#7

(PS: a friendly reminder that i+= 1 can also be written as i++ :wink:)