If it finds the first letter of my name it prints out whatever it finds


Log it! of search text for your name

I did it correctly and i passed but my code just prints out whatever comes after it finds the first letter of my name. So my name is Mike and if I put 'My name' in the text variable, it pushes out 'M' 'y' ' ' 'n' to the hits array. Is it supposed to do this?

/*jshint multistr:true */
var text = "Mike, My name is, Mike, My name is, Mike"
var myName = "Mike"
var hits = []
for (var i = 0; i < text.length; i++) {
    if (text[i] === 'M') {
        for (var j = i; j < (i + myName.length); j++) {
if (hits === 0) {
    console.log("Your name wasn't found!");
else {


In the final exercise of that lesson (7. Victory!), they mention that the search function isn't perfect.

"Now, as we mentioned, this system isn't perfect. For instance, if the paragraph contains both "Eric" and "Eddie", we'll see this in our hits array:


The search function you made using these lessons looks for the first letter of your name in the string, then the length of your name is the amount of characters it pushes into hits. So it finds a capital M, then takes that M and the following 3 characters and pushes them into hits. It doesn't check for your name - only for the first letter of your name.


That's what I thought, thank you for the help!


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