I was running the following code to get the relevant output which displays the alphabets of the word Eric.
/*jshint multistr:true */
text = "My name is Eric. Eric lives in New York";
var myName = "Eric";
var hits = [];
for (var i=0; i < text.length; i++) {
if (text[i] == "E" ) {
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);
}
I am able to obtain the desired output which is [ 'E', 'r', 'i', 'c', 'E', 'r', 'i', 'c' ]
However, when I enter "l" in line 8 of my code I get the following output: [ 'l', 'i', 'v', 'e', 's', ' ', 'i' ]
As per my understanding, the code should return a message Your name wasn't found!.
Instead it is still processing the characters after the letter l and returning it as an output.
How can I refine my code in a better way to ensure that only the characters in the string Eric are searched and returned as an output whereas any other characters will be rejected?