Second for loop


Not pushing too hits array. My code makes sense, am i missing something?

       var text = "some text is supposed to go here";
       var myName = "Lando";
       var hits = [];

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


It looks OK to me. How do you know it's not pushing to hits array? Is there an errors? And what happens if you add console.log(hits); at the end?


It returns an error....i ended up changing "===" to "=" and it acceptex
that for some reason.


Using = would cause it to return true, even if it's false. Presumably something is wrong in the if statement. You could try if( text[i] === myName[0] )


Have you tried adding parentheses around this section --> j < ( i + myName.length);


code looks correct, but

if your var text = "some text is supposed to go here";

and your var myName = "Lando";

then surely it wont push anything to the array because "Lando" does not exist anywhere within var text. right? Try adding Lando to the text and it should work fine.


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