Search Text for Your Name - Cant find my name?


In my code shown below, whenever I try to save&submit the code, for some reason the console comes back saying, "Your name wasn't found!" which is only supposed to happen when there are no hits on my name in the text. As you can see, my name, Ted, is found 4 separate times in the text. Please let me know why this code will not push my name into an array. Thank you.

Link to exercise:

My code:
/*jshint multistr:true */

var text = "Hi my name is Ted and this program is looking for the name Ted and when it finds the name Ted it will tell me when it finds the name Ted."
var myName = "Ted"
var hits = []
for (var i = 0; i < text.length; i++) {
    if(text[i] === myName[0]) {
        for(var j = i; j < (i + myName.length); j++) {
if(hits.length === 0) {
    console.log("Your name wasn't found!")
else {


this line:

if(hits = [0])

you should compare the length (use .length) of hits with zero (not a zero index of a non existing array)

Also, comparing is done using two or 3 equal signs. Not a single one (single is assign)


OK. So if I understand you correctly, I was trying to look for the first term in the 'hits' array when i used "hits = [0]" when I need to be trying to find out if I got zero hits. And I also used the wrong amount of '=' to compare. So a correct usage would be "if(hits.length === 0)". Correct?


this is actually just invalid:

hits = [0]

because you don't specify the array:

hits = text[0]

This will give hits a value of the first letter of text.

yes, if(hits.length === 0) is correct


Thanks for the explanation on what I did wrong. However, when I corrected my code, I am getting "undefined" as the message from the console. Do I need to adjust other parts of my code?


I think you're missing parenthesis around j<(i + myName.length[0]). Also you want to add the variable, not "Ted" between parens to the hits array with push.


So i updated my code, by putting the "i + myName.length[0]' in parenthesis and by replacing my name in quotes with the variable myName in the hits array with push. This now gives me the console response "Your name was not found!". Also, one question: why do you include the [0] at the end of (i + myName.length)?


Ooops. My bad. Yeah the [0] should not go after the myName.length. Typo.

I'm no expert and trying to learn this also, but you want the letters from text to go into hits. The variable "j" grabs those letters from the for statement above it. So you want the "j" of the variable text if that makes sense. = text[j]


I replaced the text[j] in the hits.push parenthesis and it still returns "Your name wasn't found!" In the console. Am i putting it in the correct place?


Only thing I see is that the last line doesn't need ['Ted']. You just want hits to print.


hits is the array which holds all instances of your name, so you only need to log hits to the console


Ok. That makes sense. Since I already defined hits as an array, all i need to print is the variable hits. I changed that and it still only says, "Your name wasn't found!" in the console.


your code now works fine, try a different browser, your browser might send cached data


Thanks so much, that fixed the issue.