Question 5!


I got the question correct through QA but I just don't understand this part
for (var p = i;p
** hits.push(text[p]);**

I don't know why we add "i" to "myName"
and I also don't understand the effects of "Hits.push(text[p]) when we have console.log- what is the use of the hits?


if im correct the hit.push things makes it so its printed out 'p', 'e', 'r', etc


I'm not sure what you mean by adding i to myName. Can you tell me more about what you mean?

push() is used to add items to an array

So what you're doing here is you have a for loop that cycles through every character found in the variable text. if it finds a word beginning with the first letter of your name it then captures that word. To capture it, it uses push to add each letter into the array hits.


Thank you!
on line 7 it says :
for (var p = 1;p<i+myName.length;p++)


In the first for loop you have a variable p. That variable is used to cycle through the text variable, it increments until it goes through the whole text length (all letters). If it finds the letter that begins with the same letter as your first name it then runs the second for loop.

For this second for loop you set p=i because that will set p to be the same number of the text array. From there you have to capture your name. In order to do that you have to put i + yourName.length as the stop point. This will make sure you start capturing at the spot you're at then add your name length to that to create a stop point for your for loop. I hope that makes sense...