5. Search Text for your name, need help!


#1

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

for(i=0;i <= text.length ; i++){
if(text[i]==="M"){

    for(j = i; j=<myName.length; i++){
        hits.push();
        }
    }
}

#2

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

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

Proplem was in your for loop. Instead

for(j = i; j=<myName.length; i++) {
   hits.push();
}

you need

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

i - is a position where 'M' is;
i + myName.length - how many times you need push matches symbols;
i++ - wrong iterator, because in nested for loop you are using j for iterations;
hits.push(text[j]) - push() method in this case should takes one parameter - it's a value that adds to the end of an array.


#3

Thank you! figured it out


#4

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