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


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";
hits =[];
for(var i=0;i<text.length;i++)
         for(var j=i;j < i+ (myName.length+1); j++)


No it is the other way round that is correct

j < i+(myName.length)

should not give an error in your code


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


@rydan @samauden

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

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


@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?