Your second "for" loop


#1


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


Error message: 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.


/*jshint multistr:true */
var text = "Blimey! There's a flotilla off the port bow! Man the  crow's nest and batten down the mizzenmast, we yelled when a box of  these came in.  Then we opened the box and realized they were just  Raspberry Pi cases from our good friends at Pimoroni. The Pibow Coupé Flotilla is the nautical blue version of the fantastically red Coupé.  Like most pibow cases, this case is laser cut from high-quality acrylic that's stacked one on top of the other to protect your Model B+, Pi 2 or Pi 3 while also allowing you access to its primary ports - including the camera and GPIO. No tools are necessary and the Coupé Flotilla is super lightweight at 61 grams. Note: As of Monday, May 16th 2016, this enclosure is now compatible with Raspberry Pi 3! Remember, this enclosure is only compatible with the Raspberry Pi Model B+, Raspberry Pi 2 and Pi 3! Raspberry Pi not included."
var myName = "is"
var hits = []
for (var i = 0; i < text.length; i += 1){
    //Checks for the letter you are on
    if (text[i] <= myName[0]){
        for(var j = i; j < i + myName.length; j += 1){
            hits.push(text[j]);
        }
    }
}


#2

This is highly restrictive. Best to define an inequality so you can iterate a range of characters.

j < ...

is not incrementing as expected. There is no assignment. We can use one of two compound operators... As you have above, n += 1; or, n++. The latter is most common and simplest to read and write.


#3

ok, thank you, I will fix that, and come back...


#4

Umm, that crashes my browser.


#5

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

I used n in my example so as not to just blurt it out.


#6

Ok, I fixed that, but I still get the
"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." error.


#7

We'll need to see your revised code, but before that, fix this line, as well...

for (var i = 0; i < text.length; i++) {

Because of zero-indexing, the last index is not the length, but just below it.


#8

Wow sh*t i'm stupid, I fixed it. Thank you @mtf !!!


#9

Ok, I edited the Parent Post, so you can see it, and now it prints a number. Hopefully fixed in the next lesson?


#10

The number is likely the last value of one of the loops or the length of the hits array. Can't remember. It's just console behavior, not output from the program.


#11

Ok, yah it is logged in the next step. Thank you for being so helpful!!!!


#12

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