"Search text for your name: your second for loop" problems


#1

When I press the submit code button, it freezes without doing anything and I don't know what the problem is with my code.

/*jshint multistr:true */
var text = "Blah blah blah blah Marina blah blah blah Marina blah blah blah blah blah Marina blah blah blah blah blah"

var myName = "Marina"

var hits = [];

for (var loop = 0; loop < (text.length); loop++)
{
if (text[loop] === "M")
{
for (var loopy = loop; loopy < (loop + myName.length); loop++)
{
hits.push(text[loopy]);
}
}
}
console.log(hits);


#2

Should be: loopy++


#3

Marina,

Those are some interesting variable names you got there...

Check if hits has a length of 0 at the end. This works toward a better user interaction.

if (hits.length==0){
    console.log("Your name wasn't found!");
}

else{
    console.log(hits);
}

Instead of just telling them 0, we tell them, your name wasn't found. :slight_smile:


#4

I got, it thanks for the help


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.