Second for loop . 5


#1



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


My code prints out 'more' and 'mihai'.
Shouldn't it just display my name?
Additionally, the first 4 lines the display normally from left to right. But after that it displays only one letter per line.
How come that is happening? It also displays my name 4 times...
Any advise is much appreciated.

[ 'm', 'o', 'r', 'e', ' ' ]
[ 'm', 'o', 'r', 'e', ' ', 'm', 'i', 'h', 'a', 'i' ]
[ 'm', 'o', 'r', 'e', ' ', 'm', 'i', 'h', 'a', 'i', 'm', 'o', 'r', 'e', ' ' ]
[ 'm', 'o', 'r', 'e', ' ', 'm', 'i', 'h', 'a', 'i', 'm', 'o', 'r', 'e', ' ', 'm', 'o', 'r', 'e', ' ' ]
[ 'm',
'o',
'r',
'e',
' ',
'm',
'i',
'h',
'a',
'i',
'm',
'o',
'r',
'e',
' ',
'm',
'o',
'r',
'e',
' ',
'm',
'i',
'h',
'a',
'i' ]


var text = "Words words, and more words for mihai and more Words, words and more words for mihai"
var myName = "mihai"
var hits = []
for (var i = 0;i<text.length; i++){
    if (text[i]==="m"){
        for (var j=i; j< (myName.length+i);j++){
            hits.push(text[j]);
            }
    console.log(hits); 
        }
       
    };


#2

@leftthumb

It will print more because it has like first letter m which is what we are searching. Since we don't have further conditions on whether all the letters matches the name, it will add whatever word that starts with m.