Confusion on The Second For Loop


#1

I have been stuck for quite a while and cannot figure out what I need to add or change. I started a few days ago and came back, but I made changes so I do not know how far off I am to the actual code, and what I need to do to fix

//jshint multistr:true//

var text = "Hey, how are you \ doing Michael? My name is Michael.";
var myName = "Michael";
var hits = [];
for (var i = 0; i < text.length; i++)
if (text[i] === "Michael") {

}

for (var j=i; i < i+myName.length; j++) {
newArray = [];
newArray.push('Michael');
newArray[0];
}

Probably also worth pointing out that in it's current state it infinitely loops and crashes the browser


#2

Hi this part

if (text[i] === "Michael")

You should put text[i] to the first letter of your name like that

if (text[i] === "M")

===========

here

for (var j=i; i < i+myName.length; j++) {
newArray = [];
newArray.push('Michael');
newArray[0];
}

You don't need to creat a newArray so remove newArray = []; and newArray[0];

This part

newArray.push('Michael')

They aske you to push text[j] to your array hits so Its should be like that

hits.push(text[j])

#3

Thanks for the help, but it still makes my browser crash. I think it has something to do with it not stopping properly when it finds my name?

Code is now:

//jshint multistr:true//

var text = "Hey, how are you \ doing Michael? My name is Michael.";
var myName = "Michael";
var hits = [];
for (var i = 0; i < text.length; i++)
if (text[i] === "M") {

}

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


#4

Here

if (text[i] === "M") {

}<== remove that

you should close your if statement after your second for loop


#5

So wait, do I need to put the entire second for loop under the if statement? I tried doing so and the browser still crashes.

//jshint multistr:true//

var text = "Hey, how are you \ doing Michael? My name is Michael.";
var myName = "Michael";
var hits = [];
for (var i = 0; i < text.length; i++)
if (text[i] === "M") {
for (var j=i; i < i+myName.length; j++) {
hits.push(text[j]);
}
}


#6

You should be like that

for (conditions) { // first for loop
    if (condition ) { //if statement 
         for (conditions ) }// second for loop
             hits.push(text[j]
         } // close the second for loop
   } //close the if statement 
} // close the first for loop

#7

It still crashes it. Are the brackets and semicolons all correct in what you just posted? I can't tell if you made a typo on the backwards bracket or if you really want me to put a closing one there, or not have a left parenthesis and semicolon after the hits.push.

One more time

//jshint multistr:true//

var text = "Hey, how are you \ doing Michael? My name is Michael.";
var myName = "Michael";
var hits = [];
for (var i = 0; i < text.length; i++) {
if (text[i] === "M") {
for (var j=i; i < i+myName.length; j++) {
hits.push(text[j]);
}
}
}

As a side note, how do you add the code so it's formatted properly in the comment, with all the spacings? I'm messing with the tools and cannot see how to


#8

The error its here

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

This part

 i < i+myName.length

Instead of i < put j < like that

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

====
Read this


#9

Thank you so much on both accounts! It's finally working now and I can progress