What is the code doing?


#1



https://www.codecademy.com/en/courses/javascript-beginner-en-XEDZA/0/6?curriculum_id=506324b3a7dffd00020bf661#


My code passes the test. I just want to make sure I understand what it is exactly doing.

  1. In the code snippet "(myName.length + i)" why do we have to add "i" to "myName.length"? Isn't "i" the first letter of your name at this point so adding the length of your name would make it 1 more than the length of your name?

  2. In the code snippet "hits.push(text[j])" I understand you are putting the data into the hits array. At this point is "text[j]" equal to the characters of your name because you have defined "j" as your name?

Thanks!!!


Replace this line with your code. 
var text = "Well hello Mark, / how are you Mark?";
var myName = "Mark";
var hits = [];
for (var i = 0; i < text.length; i++) {
    if (text[i] === "M") {
      for (var j = i; j < (myName.length + i); j++) {
       hits.push(text[j]);   
      }
    }
}
if (hits === 0) {
    console.log("Your name wasn't found!");   
} else { 
    console.log(hits);
}


#2

@arrayace58474

  1. Well, that's for looping over your entire name. If you don't put it, it will not print the last character in your name since we are incrementing I.
  2. You are putting the characters indexed by j in text variable inside your hits array.