Second for loop -> hits.push(text[j]); statement


#1



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


The code works but I want to understand it better.

hits.push(text[j]);

Can someone help me break it down piece by piece? Please correct me if I am wrong.
i - we are looping through the whole text string until we find the value "T'
hits - refers to the array that we created earlier. This is where we will be printing our text
.push - the function to add the data to the array
text - the variable text
[j] - ?


/*jshint multistr:true */
var text = " Hi there. Hi there. Hi there. Hi there. Tyler.\ Hi there. Hi there. Tyler Tyler Tfja Tylaf Tylefffds"

var myName = "Tyler"

var hits = []; 

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


#2

If you are not familiar with array mutation, then array.push() will come as a surprise but don't let it throw you. It's simply a built-in method for extending (appending) an array.

var hits = [];     // an empty array

hits.push("H");    // adds a new element to the end of the array

hits.push{"e");    // adds another new element after the first

console.log(hits); // [ 'H', 'e' ]

#3

@rawcocoabeans,
Maybe if you read
https://discuss.codecademy.com/t/i-passed-this-but-i-just-need-someone-to-explain-the-second-for-loop/30895/2?u=leonhard.wettengmx.n