6 Search Text for Your Name finds other words if first letter is same as myName


#1

There's a problem with this exercise is it doesn't address the problem when there are other words in text which also start with the same letter as myName.

If

var text = "Hello Jim. Jim likes Jelly Beans. Good bye Jim!";

Then the output is [ 'J', 'i', 'm', 'J', 'i', 'm', 'J', 'e', 'l', 'J', 'i', 'm' ].


#2

Oh never mind, I just read 6.7.


#3

This code should run:

/*jshint multistr:true */

var text = "Hello Jim. Jim likes Jelly Beans. Good bye Jim!";
var myName = "Jim";
var hits = [];

for (var 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);
}

#4

can someone explain me what the code actually does???