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


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



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


Thank you! That was it!


@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.



Thank you for the help!


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