5. Second "for" loop- Please Tell me what I did wrong


#1

I'd appreciate any help. I've scoured the forum and can't figure out what I did wrong. Anyone?

 var text="I like chocolate josh he does josh"
var myName="josh"
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]);
   
   
   }
}

#2

Semicolon after for terminates the loop.

You should correct that line:

for (var i=0;i<=text.length;i ++);

Instead of semicolon you should open a block with {.


Take a look at this condition:

i<=text.length

We are indexing characters starting from 0. This means that text[text.lenght] does not exist :smile:


#3

LOL Thanks man. spent hours on that.


#4

You're very welcome :smile:


#5

Hey @arcrunner89146,

Remember, it's a for loop. What does a loop have? Two corresponding brackets.

The semi colon that's after the for loop makes everything that's in the same line/part of the loop canceled, making the for loop useless. Take away the semi colon and add two corresponding brackets that start after the for loop and at the end. :smiley:

--EDIT---

WOOOPS BAD COMPUTER BAD

My computer didn't load, didn't see it. :sweat_smile:

Ignore this all. LOL


#6

Well, I have to go, my classes start in 40 minutes.

You can answer next question without fearing that I will be faster :smile: Have a nice day!