Wrong result instead of my name it's [ 'a', 'h', ' ' ]


#1


/*jshint multistr:true */
var text="nah nah nah nah Dan nah nah Dan fsdgdngj Dan Da D andfjg Dan lorem ipsum";
var myName="Dan";
var hits=[];

for(var i=0;i<text.length;i++)
{
    if(myName[i]===text[i])
    {
        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);


The result is: [ 'a', 'h', ' ' ]
So can you tell me what i did wrong?


#2
if (myName[0] === text[i])

#3

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