Your second 'for' loop isn't pushing values to the hits array


#1



I'm stuck :[

var text = "Lentz is the most awesome guy in the universe, Lentz is awesome, Lentz is cool";
var myName = "Lentz";
var hits = [];

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


Oops, try again. It looks like your second 'for' loop isn't pushing values to the hits array. Make sure it's working properly and that myName's text appears somewhere in the text variable.


Replace this line with your code.


#2

This code block:

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

should be inside your first for loop:

for (var i = 0; i < text.length; i++){ /* code block here */}

take a look at both loops:

for (var i = 0; i < text.length; i++);

you have a ; at the end, don't! this will end the loop early. (this goes for both loops)


#3

Sorry I'm a total noob at this, so when would it be appropriate to end a loop with a ; ?

Thanks for the fast reply! :slight_smile:


#4

var text = "Lentz is the most awesome guy in the universe, Lentz is awesome, Lentz is cool";
var myName = "Lentz";
var hits = [];

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

So by taking off both ; at the end of the loop, I was able to fix it.

Cheers stetim94 for the help! :smile:


#5

but now nothing is nested inside your first for loop:

for (var i = 0; i < text.length; i++) <!-- missing { -->
    if (text[i] === "L") {
        for (var j = i; j < myName.length + i; j++) {
        hits.push(text[j]);
      }
}
<!-- missing } to close your first for loop -->

you also might want to read this:

it will cause your indent to show on the forum


#6

Thank you! I've fixed the problem :smiley:


#7

7 posts were split to a new topic: Second for loop problems


#12

4 posts were split to a new topic: Your second for loop isn't pushin values into hits array