Wrong output. Can you please tell me what's wrong with my code?


#1



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


Other letters are being pushed too. I don't know why.

My output shows: [ 'A', 'm', ' ', 'I', ' ', 'N', 'a' ]


/*jshint multistr:true */
var text = "Am I Naoreen? Is it Naoreen? They call me Naoreen!";

var myName = "Naoreen";

var hits = [];

for(i = 0; i<=text.length;i++){
    if(text[i] === text[0]){
        for(j = i; j<i + myName.length; j++){
            hits.push(text[j])
        }  
    }
    
};

if(hits.length === 0)
{
    console.log("Your name wasn't found!");    
}

else
{
    console.log(hits)
        
}


#2

your if condition in the loop:

if(text[i] === text[0])

you want to find the first letters of your name (myName) inside text.


#3

Thanks a lot. I got it :slight_smile: