Your second for loop


#1

What's wrong
I cannot find out what is wrong it is always giving this error:

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

This is my code:

/*jshint multistr:true */

var text = "Jonathan";
var myName = "Jonathan";
var hits = [];

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

    }


}

#2

It's supposed to be var j = i; not j = i. Also, I believe that you are supposed to use j, so

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

Also, there's also the hits array anyways, so you don't have to make another one here:

Take this out also:

Don't know what that's for.... also, you're supposed to push text[j], like this:

hits.push(text[j]);

For we have to say the location of it.


#3

The loop within the if statement:

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


#4

Thanks for that it really worked!!!


#5

For some reason that I don't understand, mine just doesn't work, please help :frowning:

/*jshint multistr:true */
var text = "Hello, how are you?";
var myName = "Paulette";
var hits = [];

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


#6

You accidentally fused the if loop and for loop together over there. :smiley:

Just separate them would do a good job.:smiley:

Also you're supposed to push j to hits, not i. :smiley:


#8

When I copied the code to the forum, for some reason it got shorten or something. This is my code... and still not working :(!!

/*jshint multistr:true */
var text = "Hello, how are you?";
var myName = "Paulette";
var hits = [];

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


#9

I believe that it's supposed to be j < myName.length + i instead of j < myName.length....


#10

I was missing my name in the text!
Thank you!!