I don't get it, I get "Your name wasn't found!", but my name is in there


#1

Hello you guys, this is my code for the "Log it!" lesson.

Although I got a correct answer in the previous one, now when I'm submitting the code, I get "Your name wasn't found!"

Can someone point where is my mistake?

Thanks.

var text ="Blah blah blah blah \
blah blah John blah blah blah \
blah blah John";

var myName = "John";

var hits = [];

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

{
    if (text[i] === "J");
    {
        for (var j = i ; j < (i + myName.length) ; j++)
  {  
      hits.push(j);
   } 
   }
    
}

if (hits === myName) {
    
    console.log("Your name was found!")
}
else {console.log("Your name wasn't found!")
}


#2

this line:

if (text[i] === "J");

dangerous semi-colon, it prevents the if condition from working properly.

This line:

hits.push(j);

j is just a index number, you will need to use text[j] to get letters

this line:

if (hits === myName)

i am not sure what you are doing, you should check if the length of hits is zero (see hint)

You swapped it, else should run if your name is found (in which case i would log hits)


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.