5/7 Your second "For" loop


#1

I can't tell what's wrong:

/*jshint multistr:true */

var text = "ianianin ainai, joe, ainianina, joe";

var myName = "joe";

var hits = [];

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

It says "Careful: your second 'for' loop should stop when it reaches its current point in the string + myName.length."


#2

Read the error message again.

You have to change condition part of your second for loop (j < myName.length).

Current poin in the string is represented by variable i.


In the second for loop you want to iterate through letters of your name. Starting from index i to the i + myName.length index.


#3

Like this?

/*jshint multistr:true */

text = "ianianin ainai, joe, ainianina, joe";

var myName = "joe";

var hits = [];

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


#4

Nvm, I got it. Ty (I realize there was a greater than sign rather than a less than sign, fixed that, and it worked)


#5

Glad you figured this out!

You're welcome :smile: