Where did I go wrong?


var text = "Hello, my friends call me\
Ringo Jackson. I love when\
they call me Ringo Jackson";

var myName = "Ringo Jackson";

var hits =[];

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

  1. You should use <, not <=. Say, text.length is 2. If you use <=, the code will be executed for i equal to 0, 1 and 2 (that is, 3 times). But, again, we've got only two letters in our "text" variable, so we need it to run only two times. So we use just < to decrease the number of iterations by 1.
  2. You should have written the second "for" loop this way:

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

The program has to put letters which it finds in the "text" variable, not "myName", into the "hits". That means, we want to move to the right for as many letters as the "myName" variable consists. But j equals to i initially, so the last letter we "push" will have number of myName.length + i.