5. your second for loop - what does '.push' actually do



i dont understand most of what im doing to be honest, even if i get it right. i spent the beginning of learning how to code dipping in and out too much, not paying attention, learning what to do but not understanding why you actually do it. this is a problem i obviously need to sort out sooner rather than later.

but anyways. the problem at hand:
so i basically didnt understand what to do at all for this section, because i dont understand what .push does and why we need it.

i thought looking at other peoples code my help me understand, but i still dont.

the correct answer is:

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

and i THINK i understand up until hits.push(text[j]);
basically. what is hits? what is it doing? and why are we pushing text[j] to the end of it?

maybe thats the main problem? im not sure what hits is meant to represent?

not sure what else i can say on that subject, hope thats clear!

but if anyone wants to help me for my other questions to clear up some of my general confusion then thatd be great!
if you want to ignore the following and just answer my first question thatd be fine too.


another question i have directly related to this section is, out of interest, would we, for instance, just be able to swap the variables we have defined when we use them in the for loops? i mean, if i could search var hits for var text, and then do myName.push(text[j]); ?
obviously you wouldnt find anything, but would it run successfully/be valid?

maybe thats an odd question but i feel like knowing would clear some things up for me


a big thing im confused about is the abstractness of what we are actually doing. what is the context of this lump of code? where did it come from? what is it affecting?i know that doesnt make much sense but i just dont understand what bearing this couple of paragraphs has on anything. how would it be placed on a website? for this topic i.e. searching for a certain phrase in a body of text, would this code be run if you press ctrl + f for example? and then somehow the computer knows to make all of the body of text on the webpage be var = text?

should i just not be worrying about that in this stage?



This piece of code adds the text variable indexed as j variable to hits array.

Sorry I don't understand your next question...


thank you for replying! ok great but why do we want that? what is hits going to do with it?
uhm as for the next question do you mean the "MORE GENERAL STUFF"? because to be honest after writing it out it seems kind of silly and im not really sure i understand what i meant either. maybe lets not worry about it because i think just writing it out cleared some things up for me.



The hits array is collecting your name in the text variable.

Hope this clarifies your understanding :slight_smile:


so ! i find the first letter, when the letter in postition i in text = O , then j takes the same position as i, O gets pushed to hits, so basically... weve found O?

ok, i get what it does kind of now. but then the next page says my code will:
"push that letter and all the letters that follow it to an array, stopping when the number of letters it pushes are equal to the number of letters in your name."

so the second for loop pushes the next letters to hits individually, one per loop, until supposedly it has collected my name? what if its not my name, just an O and then some gibberish the length of the rest of my name?
sorry to be dense!




Well, it will enter all the letters of the word inside the hits array. Sorry, but if the for array sees the first letter of your name, it will still enter it inside the hits array because it doesn't test if the rest of the letters in the word are equal to your name.


oh ok i see! thank you