Help with: Oops, try again. It looks like your second 'for' loop isn't pushing values to the hits array


#1

Hi all,

I'm getting this error:

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.

This is my code:

    /*jshint multistr:true */

var text = "Hey, how are you \
doing? My name is Emily.";

var myName = "Rob";

var hits = [];

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

I can't for the life of me see what's wrong with it. Can anyone offer some advice? Thanks so much,

Rob


#2

I think you need to have your name "Rob" in the text to be able to push it?


#3

That's a great piece of advice! Thank you. I tried it again as below but frustratingly I have the same result.

/*jshint multistr:true */

var text = "Hey, Rob, how are you \
doing? My name is Emily.";

var myName = "Rob";

var hits = [];

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

#4

DANG! For future reference this may help someone. I had my <= the wrong way around in the for loop. Here's the working copy.

/*jshint multistr:true */

var text = "Hey, Rob, how are you \
doing? My name is Emily.";

var myName = "Rob";

var hits = [];

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

#5

I think your issue is here: i>=text.length;

It says: continue looping for as long as i is greater or equal to the number of characters in the text. This would be for just the last character. Try this: i < text.length;