Stuck on the second for loop


#1


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


var text = "Sic Semper Tyranus";
var myName = "Ryan";
var hits = [];

for (var i = 0; i < text.length; i++){
    if (text[i] === "R") {
        for (var j = i; j < i + myName.length; j++) {
            hits.push(text[j]);
            }
        }
    }


#2

I suggest adding prints in your code to see where it goes wrong.
A good place to start would be to print out the array to see if it is indeed empty after running the rest of the code.

> var text = "Sic Semper Tyranus";
> var myName = "Ryan";
> var hits = [];
> 
> for (var i = 0; i < text.length; i++){
...     if (text[i] === "R") {
.....         for (var j = i; j < i + myName.length; j++) {
.......             hits.push(text[j]);
.......             }
.....         }
...     }
> hits
[]

As you see, hits is still an empty array, so one would continue by adding prints to investigate whether the loops run


#3

I figured out what the problem was...my name wasn't actually in the "text" variable. Thank you for your help though


#4

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