Not understanding any part of the code


for(var j = i; j < (myName.length + i); j++) {

  1. Why is it myName.length + i? Why the plus i? I don't know what it's trying to accomplish here.
  2. What is hits.push(text[j]);? Is it adding "j" into the text? Why are we modifying the text we're scanning when we just want to find if my name is in it?

Also looking at the full code, how does it know what the rest of my name is? I know we set myName = Eric but we don't ever tell it to look for "Eric", only the letter "E". Why are all the other words starting with "E" not being displayed too? EDIT: Ok, it does show all other words with the same length and starting letter. I understand this part but still don't get the first part.

  1. You are adding the index. If you just did myName.length it wouldn't have the intended output. Example:
    Pretend that text is "Hi my name is Cade"
    If you looped through without + i every hit found would be "Hi m"

  2. It's adding the letter into the hits (or found) list.

  1. I'm not understanding this. What do you mean by "adding the index"? Also, I'm not sure how you would get "Hi m" by leaving out the + i.
  2. Ah, I get it thanks!


It would do the length of it at the start. Cade is four indices and the first four indices would be "Hi m" H i (Space) m.


I still don't get it unfortunately. What exactly is the computer getting from + i? The + i command meaning doesn't make sense to me.

  • i adds where you are in text. Letters, numbers, symbols, and spaces all take up 1 index. Here is text:

H i   m y   n a m e   i s   C a d e
0 1 2 3 4 5 6 7 8 9 10 
*                    11 12 13 14 15 16 17

You loop through, and find "C" at index 14. You add 14 to the length of name (4, in this case) which adds to hits ("C", "a", "d", "e"). If you didn't add i (14) then it would be less than 4, which is always "Hi m" which takes up the first four indices.


Ohh I get it! In your example where you said "add 14" I was thinking add 14 from where the name beings but add 14 to the name length so that from the very beginning it gets to the end of the name.



I am still a little confused because if i = j then j should start from "C" and if the second part of the loop indicates where it should finish at "e" or name.length which is 3 since it is starting from "C". I only understand that the function will print out the starting letters of the sentence as long as the defined name if you do not add i to the name.length . But i cannot understand why it does that since i is equal j and i starts from "C". Am i stupid or am i missing something here ?


i is equal to "C" because you are looping through to find "C".