Search text for your Name exercise- facing difficulty in understanding the second loop logic of j < (myName.length + i)


#1

Hello Friends,
I am doing the search string for name exercise. The below is my code. I am facing difficulty in understanding why we we need to put j < i+(myName.length+1) instead of j < i+(myName.length). I have put j < i+(myName.length) as per my understanding and it is giving an error and when i changed to j < i+(myName.length+1) it is working. I am not clear why we need to add plus 1.I request all friends to help me and clarify on this

var text,myName,hits;
text ="samauden is in newyork and now working as software engineer \
samauden has moved from chicago to newyork for a better job.He works on \
latest techniques of digital marketing";
myName="samauden";
hits =[];
for(var i=0;i<text.length;i++)
{
    if(text[i]==="r");
     {
         for(var j=i;j < i+ (myName.length+1); j++)
           {
               hits.push(text[j]);
           }
     }
}

#2

No it is the other way round that is correct
using

j < i+(myName.length)

should not give an error in your code


#3

@rydan. It gave me error. when i changed to j < i+(myName.length+1). Now corrected, it's working. Thanks


#4

@rydan @samauden

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

This expression can also be used to get the same effect.


#5

@sterngw. I think the logic of ` j < i+(myName.length) is correct instead of j <(myName.length + i). I don't understand why we need to add* +1 or +i (i=1) to the myName.length*. Could you let me know?