5. Your Second "For" Loop- SyntaxError: Unexpected token


#1

Can someone explain what I'm doing wrong here? I keep getting "SyntaxError: Unexpected token".

/*jshint multistr:true */
var text = "blak blah Brianne Brianne blah blad Brianne blah";
var myName = "Brianne";
var hits = [];

for (var i= 0; i < text.length; i++){
if (text[1] === 'B')
{ for (var j = i); j < (myName.length + 1); j++){
hits.push(text[j]);
}
}
}
if (hits.length is 0) {
console.log("We can not find your name...")
}
else
{ console.log(hits);}
}


#2

When writing your for loop, you added an extra closing parentheses.
Instead of
for (var j = i); j < (myName.length + 1); j++)
it should be
for (var j = i; j < (myName.length + 1); j++)

That should solve the syntax error. But you will also have trouble because your loop will stop too soon. Because text is longer than myName, i will eventually be larger than myName.length. This means that your second loop will eventually be starting with j already larger than myName.length. To solve this, you'll have to make sure your second loop runs until j < (myName.length + i)


#4

Try this code
var text = "blak blah Brianne Brianne blah blad Brianne blah";
var myName = "Brianne";
var hits = [];

for (var i= 0; i < text.length; i++){
if (text[i] === 'B')
{ for (var j = i; j < (myName.length + i); j++){
hits.push(text[j]);
}
}
}
if (hits.length === 0) {
console.log("We can not find your name...")
}
else
{ console.log(hits);}
}


#6

var text = "Hi stupid user";
var myName = "Boris";
var hits = [];

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

    }
}

}
if (hits.length === 0) {
console.log("We can not find your name...");
}
else{
console.log(hits);
}
Can you tell me what's rong of this code i'm stuck here.