Second 'for' loop isn't pushing values to the hits array


#1


6. Search Text for Your Name


Heres the error that i'm getting : "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."

What am i missing here?


/*jshint multistr:true */

var text = "Hey, how are you \
doing? My name is Rick. Funny stuff with \
Rick, Fun and games with Rick";

var myName = "Rick";

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] );
        }
    };
}
console.log(hits);


#2

Hi there.

Just one simple little mistake is all it took to get this error.

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

We should be testing whether i is less than text.length. Not greater than. Currently this for loop will never run because 0 is definitely not greater than text.length :wink:

Hope that clears things up.


#3

here is my version ... same error ...

var text = "Hey, how are you? / My name is Med.";
var myName ="Alex";
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])
}
}
}


#4

i;m an idiot :)) this version works ... just that myName has to be the same name as in text :slight_smile:

he you go

var text = "Hey, how are you? / My name is Med.";
var myName ="Med";
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

Thank you so much.. Worked perfectly :blush:


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.