Please help me on "Your Second For Loop" - Search Text For Your Name


#1


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


My code actually works however I'm not totally understanding a concept. In the exercise in the second for loop we have to use (i + myName.length). How would I know when to include an ' i + ' ? Any insight would be greatly appreciated.


/*jshint multistr:true */
var text = "bla bbla bla blba bll hi, how are you blba bla bla bla bla bla Kevin blab bla hi ahow bla bla bla bla bla";
var myName = "Kevin";
var hits = []
for (var i = 0; i < text.length; i++) {
    if(text[i] === "K") {
        for(var j = i; j < (i + myName.length); j++) {
            hits.push(text[j]);
        }
    }
};


#2

The 2nd for loop condition is j<(i+myName.length) so that the total # of characters pushed into the hits array matches the total # of characters in the myName variable. If the 2nd for loop condition was only j<myName.length, that would only push K,e,v,i into hits since that is one less than myName.length.


#3

Biff!

Thank you! Duh, that makes a lot of sense. I really appreciate it.

Kevin


#4

Glad to help :slight_smile:
I'm currently stuck at object constructors :grin:


#5

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