Your second "for" loop .... HELP!


#1

Can someone tell me what is wrong in my code? When i run it says "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.
"


1. /*jshint multistr:true */
2. var text = ("StringString StringString StringString String String String Arman Arman askldjhakld aslkhjqwiou Arman kljas Arman a;sljkdl Arman");
3. var myName = ("Arman");
4. var hits = [];
5. for (var i = 0; i<text.length; i++ ){
6.     if(text[i] === myName) {for (var i = 0; i < text.length; i++){
7. if (text[i] === myName[0]){
8.     for (var j = i; j < (i + myName.length); j++){
9.         hits.push(text[j]);
10.     }}}}}

#2

@methodmaster25905,

The structure of your code should look like

for ([initialization]; [condition]; [final-expression]) {
   if ( condition) {
       for ([initialization]; [condition]; [final-expression])
            hits.push( text[j] ) 
       }
    }
 }

#3

text[i] is one letter and myName is a string of in this case 5 letters so they will never match. Also why do you have parts of your structure doubled?


#4

It says "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."

I'm too dumb for this ...

/*jshint multistr:true */
var text = "blah blah blah blah blah blah Mike blah blah blah Mike blah blah blah blah Mike blah blah blah blah blah blah Mike";
var myName = "Mike";
var hits = [];
for(var i = 0; i < text.length; i++) {
    if (text[i] === "M") {
        if(text[i] === myName) {
        for (var j = i; j < (myName.length + 1); j++)
            hits.push(text[j]);
        }
    }
};

#5

Sry for the late reply the problem seems to be this one:

for (var j = i; j < (myName.length + 1); j++)

it needs to be i+myName.length not +1.


#6

space your code it helps you debbug ur code easily