JavaScript Your second for loop


#1

Can someone tell me what I'm doing wrong and why it wont run? I've been reading and my code looks exactly the same as people's solved version so I'm stuck.
Here is my code:

/*jshint multistr:true */
var text = "Have you seen Kelly? Kelly said that she would \ be here at two. Let Kelly know I was looking for her.";
var myName = "Kelly";
var hits = [];
for(var i = 0; i < text.length; i++);
if (text[i] === 'K'){
for (var j = i; j < i + myName.length ; j++)
hits.push(text[j])
}

Thanks


#2

instead of the semi colon at the end you need to nest the contents of your for loop in an opening and closing bracket.

Same here,

Should work after correcting that. :slight_smile:


#3

What do you mean? I played around with it a little but it now gives me syntaxError


#4

please show me your updated code. :slight_smile:


#5

/*jshint multistr:true */
var text = "Have you seen Kelly? Kelly said that she would \ be here at two. Let Kelly know I was looking for her.";
var myName = "Kelly";
var hits = [];
for [(var i = 0; i < text.length; i++)]
if (text[i] === 'myName[0]'){
for [(var j = i; j < i + myName.length ; j++)]
hits.push(text[j])
}

i dont get why we need the brackets because on other peoples i dont see them being used anywhere except for putting the variables "i" and "j" in them.


#6

You misunderstood me completely.:sweat_smile:

Javascript brackets are these ones

{}

So what i meant was put one after this line like this,

for (var i = 0; i &lt; text.length; i++) {

And after this loop like this,

for (var j = i; j &lt; i + myName.length ; j++) {

Note: You should close both of these brackets after hits.push with two brackets like this,

   }
}