Code:
text = "Blah blah blah blah blah blah Tim \
blah blah blah Tim blah blah Tim blah blah \
blah blah blah blah blah Tim";
var myName = "Tim";
var hits = [];
for (var i = 0; i < text.length; i++){
if (text[i]== "T"){
for (var j = i; j < (myName.length + i); j++){
hits.push(text[j])
}
}
}
if (hits.length === 0){
console.log("Your name wasn't found!");
} else {
console.log(hits);
}
Hi! This is basically a small project to search the text above and to move the letters of a name to an array using for loops. I've constructed the code in a specific way pertinent to the exercise I was doing on CodeAcademy, but I just don't understand this part - (myName.length + i). I don't know why I have to put + i there, why can't I just tell the code to stop running when the letters that have been moved to the array are EQUAL to myname? I hope that makes sense.