# Searching text for your name

#1

I am not good at logic problems so even it showed that I am JS master but actually I am not. =(

Can any one explain to me that what is the purpose to`+i`in this code? `j<(myName.length+i)`

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

#2

this program your building, allows you to find your name in a `text`

so we start by looping over `text` and compare each letter in text with the first letter of your name (this is done by the if condition)

the moment the first letter of your name is found, `i` will contain the index of the first letter of your name

now we need a way to loop from the first letter of your name (`i`) to the last letter, Which is `i` + `myName.length` (given what we want to find is your name, its also perfect to use the length of myName as stop value)

#3

Thank you very much.
However, I am still a bit not sure that isn't ˋmyName.lengthˋ already contents every character of my name?
Is it because I set the ending as less than ˋmyName.lengthˋ? ˋ// j<(myName.length+i) ˋ
What if I set it as `j<=(myName.length)` ?
Do I still need to `+i`?
(and since `i` has already contained the index of the first letter of my name after checking from the text, isn't it now equal to `var j=i` in my second `for loop` ?

#4

yes, but we need a stop value for the second loop (which is finding your name in `text`string)

then we would also get the character after your name, for example:

``````var text = "Papa "
result = [];
for (x = 0; x < myName.length; x++){
result.push(text[x])
}``````

now your name is perfectly pushed, if you would use `<=` the space would also be pushed in to the array

i should actually have written the for loop in the example above as:

``````for (x = 0; x < myName.length + 0; x++){
result.push(text[x])
}``````

because it allows me to explain the use of `i`, in the example above `x` starts at 0, but what if it doesn't? Then we need to move the stop value further back:

``````var text = "_Papa"
result = [];
for (x = 1; x < myName.length + 1; x++){
result.push(text[x])
}``````

see? we want to skip the first character, so we need to manipulate the stop value. Hopefully do this simplified examples help, but instead of `1` we use `i` in our search program, because we don't know what index our name will be at

Here are some more extensive explanations i wrote a while ago: