Your second "for" loop >> not pushing values to array


#1


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


Hello! I'm getting stuck on the second for loop exercise and I can't seem to understand why. I've browsed through other people's issues and have tried to find any missing "{" or misplaced semicolons, but I can't seem to find any obvious issues.

When I run the attached code I get an error that says:

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.

Any help would be much appreciated!


/*jshint multistr:true */

var text = "Hey I'm Tim. I believe I'm nice.";
var myName = 'Tim';
var hits = [];

// This creates a loop, which goes through each character in the var 'text'
for (var i = 0; i < text.length; i++) {
    // This creates an if statement, looking at each character and comparing it to the first character of myName
    if (text[i] === 'T') {
        // If the character in text matches myName, then it creates a new loop, which starts at the location of the first letter of myName, then continues while j is less than the location of the first letter, plus the length of myName
        for (var j = i; j > (i + myName.length); i++) {
            // This pushes the character in text to hits
            hits.push(text[j]);
        }
    }
}

console.log(hits);


#2

In your second for loop you increment i by using i++. Trying to increment j using j++ instead.


#3

Thank you! That was it!


#4

@timtrautman, other than what @vjhawkins mentioned, j++ should be used,

you also need to take note of the > or < symbol which I highlighted, should it be more than or less than? careful of infinite loop.

:slight_smile:


#5

Thank you for the help!


#6

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