Baffled by 2nd for loop


#1


I'm totally confused as to why my 2nd for loop isn't working, have tried with jshint and it isn't giving me any errors so I can't figure out where I'm going wrong.

var text = "rdfaf rtsbgrthyt Andy esrhytn sdfsgt Andy dfbiojfdn rtbhrskj";
var myName = "Andy";
var hits = [];

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


#2

Have you checked if the for-statement is executed at all? Maybe it's "working" just fine, but is never executed. If so, what determines if it executes?


#3

Thanks Ionatan, the error message I'm getting is

"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 don't understand how I can check it other than by running the code?


#4

Make it print out messages about what it does


#5

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