Can't figure out where I'm going wrong here


Here is what I have, I can't figure out why it's not working, even after comparing it to other posts on here. Any help would be appreciated.

var text = ("My name is Tim and I'm Canadian. Canadian Tim");
var myName = ("Tim");
var hits = [];

for (var i = 0 ; i < text.length; i++) {

if (text[i] === "T") {
for (var j = i; j < i + myName.length; j++) {
newArray = [];


EDIT: please forgive me. I copy/pasted the example they gave, when I should have been using "hits" instead of "newArray". To be honest I'm still really uneasy on this stuff so any explanation as to WHY this worked would be great! I'm sure it will be a eureka moment when I figure it out, but for now I'm extremely confused.


You nested your newArray inside of the loop, which resets its value each iteration.

What is this supposed to do?


We do not need to wrap assignment values/objects in parentheses. They are already one type or another and easier to read without the parens.

var myName = "Tim";

Same for text. I recommend a longer string with line breakes as shown in lesson 1. Seed your name in a few places of pseudo text (eg. lorem ipsum).

Be sure to leave the comment at the top of the code. That's what allows the line breaks. \ is known as the continuation operator in this usage.

Now given that we have a suffiently long string and the name is present, we will match the first letter at some point during iteration of the string. We can use the more dynamic approach, myName[0] so it works for any name, but "T" is fine.

The outer loop iterator variable will have a value, which is the index in the string of the matched letter. This will trigger the inner loop.

We know the position of the first letter, and we know that the name has a set length, so all we need to do is iterate over text[i], text[i+1] and text[i+2] to push the three letters of your name onto the hits array. Hence,

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