6. Search Text for name. code is accepted, but is incorrect


#1


my code passed the test, but doesn't doesn't work. It just displays the whole thing and a lot of undifined. I don't see where I went wrong.


var text = "'Donk mems live on' by James";
var myName = "James";
var hits = [];
for (i = 0; i < text.length; i++) {
if (text[i] === "J"); {
    for (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);
}


#2

Dam I had the same issue, was pulling hair from my head. The reason why it happens is bc of ";" that you have after 2nd loop. Also after "IF"s you should not have ";". remove them all and you should be fine.

var text = "'Donk mems live on' by James";
var myName = "James";
var hits = [];
for (i = 0; i < text.length; i++) {
if (text[i] === "J")  {
    for (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);
}

#3

I would have never thought of that! How long did it take you to figure your problem out originally


#4

yup, It took some time lol


#5

thanks... I have the same problem


#6

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