5. Your Second "for" Loop


#1

/*jshint multistr:true */

var text = "Eyy what's going on gurl how you doin' Christian, who happens to be myself.";
var myName = ("Christian");
var hits = [];

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

What's wrong with this code? I get the SyntaxError: Unexpected token error.


#2

Hi, remove the ; after the for loop

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

#3

Also add another } at the end to close off your for loop


#4

I have also a question:
There is an error but where ?

var text = "Christian dneondfeolfe Christian nvfoirnvon Christian";
var myName = "Christian";
var hits = [];

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

And this is the error code:
Oops, try again.
Careful: your second 'for' loop should stop when it reaches its current point in the string + myName.length.

I hvae no idea at the moment how i can solve this problem


#5

Hi this part

if( text[i] === myName)

You should put text[i] equal to the first letter of your name.


#6

HI, i did that, or better i did the change to
if( text[i] === "C")
but the error message is still the same. any ideas ?


#7

Ho this part

for ( var j = i ; j <= myName.length ; j++)

You should also add i to myName.length like that

for ( var j = i ; j <= i + myName.length ; j++)

Its maybe nothing but try to remove the = to <=


#8

ok got it, this was the solution.. add

hits.push(text[j]);

in the for loop
cheers