Wrong Output


#1

https://www.codecademy.com/en/courses/javascript-beginner-en-XEDZA/0/6?curriculum_id=506324b3a7dffd00020bf661

This is the output am getting. Where am I getting wrong ?

[ 'M', 'y', ' ', 'n', 'a', 'm', 'M', 'a', 'n', 'i', 's', 'h' ]

/*jshint multistr:true */
var text = "Hey, how are you \
doing? My name is Manish.";
var myName = "Manish";
var hits = []
for (var i=0; i <= text.length; i++) {
    if(text[i] === "M") {
        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

My is also spelled with uppercase M, so it will also find that one.

the easiest solution would be to write My with lowercase m


#3

But why is it selecting all letters following that and skipping 'e' and 'i' of 'name' and 'is'


#4

this:

 'M', 'y', ' ', 'n', 'a', 'm'

comes from:

My name

it is still using your names length


#5

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