7/7 - Passed but won't print my name?


#1

Hey guys,
I've looked through the entire thread & can't find anything similar. My code "passed" all the way to the end, but instead of printing my name it either prints: [ 'C', 'C' ] when entering hits.push(text[i]); or [ '.', ' ' ] when entering hits.push(text[j]);
Here's my syntax:
/*jshint multistr:true */
var text = "My name is Cindy. The name Cindy means \
moon.";
var myName = "Cindy";
var hits = [];
for (var i = 0; i < text.length; i++){
if(text[i] === myName[0]) {
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);
}

Thanks for your help/feedback!


#2

Your inner for loop has a semi-colon after the conditional statements: " for (var j = i; j < (myName + i ); j++); { "

Remove that semi-colon and it will execute as expected.

The way you have it written is effectively this:

for (var j = i; j < (myName + i ); j++) {
//do nothing
}
hits.push(text[j]);

Which is why you are seeing the "," and " " being output.


#3

Somehow I knew it would be an extra semi-colon somewhere! Thanks so much!!! :smile:


#4

I had the same issue with the superfluous semicolon. Thanks for helping with this! Could you explain why

hits.push(text[j])

would output the punctuation like it did?

Does the semicolon cause the counter to jump to its final working value (j === i + myName.length) so that that value is stored in the variable j when it is referenced?