Second for loop not pushing, can't figure out problem


#1

This 6.5 Your second "for" loop lesson.

/*jshint multistr:true */
var text = "My name is Rachel. Do you know Rachel?  Why do you know Rachel? Is Rachel a funny person?"
var myName = "Rachel"
var hits = []
for (var i = 0; i < myName.length; i++){
    if (text[i] ==="R"){
        for(var j = i; j < (myName.length + i); j++) {  
            hits.push(text[j])
        }
    }
}

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.

I would like a second pair of eyes to help me figure out my error. I have been at this for awhile now and I can't figure out my error. It is probably something very obvious. Thank you


#2

I've fixed your indentation just so it's clearer what's happening.

I believe the problem is your first for loop - if you look at the parameters, i will only increment while its value is less than myName, rather than text. This means that it'll only loop through text while i is less than the length of the string Rachel.

Also bear in mind that it's good practice in Javascript to end every statement with a semi-colon.


#3

Thank you for your help and advice!


#4

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