5. Your second "for" loop


#1

Hey guys, I'm having some difficulties with this code, I've tried to look through all the other posts but I can't seem to find the problem, maybe you can?

var text = "Bla bla bla bla bla bla \
bla bla bla bla bla Johan";

var myName = "Johan";

var hits = [];

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


#2

@netmaster16552

Problem with typo here. Remove the ; after the parentheses.


#3

Hey, I've tried that but my browser seems to freeze when i try to run it, not really sure what's causing it.


#4

@netmaster16552

Can you post the updated version of your code please?


#5

I have the problem with or without the semicolon which is weird.

var text = "Bla bla bla bla bla bla \
bla bla bla bla bla Johan";

var myName = "Johan";

var hits = [];

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


#6

@netmaster16552

You shouldn't be incrementing i variable again. You should be incrementing j variable otherwise you are generating an infinite loop.


#7

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

}
}


#8

Thanks, that solved it. However now I get
"Oops, try again. Make sure you're pushing letters to the hits array!"

code:

var text = "Bla bla bla bla bla bla \
bla bla bla bla bla Johan";

var myName = "Johan";

var hits = [];

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


#9

@netmaster16552

Do you have this code before declaring variable text?

/*jshint multistr:true */

#10

Hi this part you should remove the ; after the condition

for (var i = 0; i < text.length; i++); <== this
if (text[i] === 'J'); <== this one too

@miniapple8888 the /*jshint multistr:true */ it's just a coment


#11

thanks, that solved it! :slight_smile: