Second for loop


#1


https://www.codecademy.com/en/courses/javascript-beginner-en-XEDZA/0/5?curriculum_id=506324b3a7dffd00020bf661#


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++) {
            hits.push(text[j])
            }
        }
    }


#2

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?


#3

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


#4

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] )


#5

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


#6

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.


#7

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