Stopping with second For loop


#1


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


I need to stop pushing to the array hits[] using my second For loop. I can't figure out how. I added a if statement within the second for loop to do it but that not what the exercise wants.
Error:
Oops, try again. Careful: your second 'for' loop should stop when it reaches its current point in the string + myName.length.


1/*jshint multistr:true */

var text = " Hello my name is Steve. I like to work on boats\
            in my free time. I've helped build or rebuild\
            several boats in my life time. My favorite was\
            the Bertram 25 I  worked on last year. Until\
            next time, I'm Steve."

var myName = "Andre";

var hits = [];

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


#3

If you need to exit a loop while it is running, you can use the keyword break followed by a semicolon to end the statement.

I see you're starting j at 1 any time an occurrence of S is found, then you're pushing to hits the incremented value of i. This is confusing, and will not return the result you're looking for.

So, what you want to do is actually start j at i, so that you can search the text in place any time an occurrence of S is reached and follow on until the length of myName is reached thereby causing each entry into hits to be the exact length of myName. You will need to push the value of text[j].


#4

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