Finished lesson 6 but still doesnt understand


#1
/*jshint multistr:true */

var text="dupa dasklsad dsakldsa dmklv rewporew artur eriwo fsdio artur dupa aga pithfpa dasjlkdsa dasjlk dasl dasl dasj artur ldksa daslk stefa fdsl fdslflsdfsdl"

myName="artur";

var hits=[];

for(var i=0; i<text.length; i++){
    
    // The if statement will say: if you see something interesting, push that text into an array!"
    if (text[i]==='a'){
        for(var 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)
      }

but when i click submit, console show:
[ 'a',
' ',
'd',
'a',
's',
'a',
's',
'k',
'l',
's',
'a',
'd',
' ',
'd',
's',
'a',
'k',
'l',
'd',
's',
'a',
' ',
'd',
'm',
'k',
'a',
'r',
't',
'u',
'r',
'a',
'r',
't',
'u',
'r',
'a',
' ',
'a',
'g',
'a',
'a',
'g',
'a',
' ',
'p',
'a',
' ',
'p',
'i',
'p',
'a',
' ',
'd',
'a',
's',
'a',
's',
'j',
'l',
'k',
'a',
' ',
'd',
'a',
's',
'a',
's',
'j',
'l',
'k',
'a',
's',
'l',
' ',
'd',
'a',
's',
'l',
' ',
'd',
'a',
's',
'j',
' ',
'a',
'a',
'r',
't',
'u',
'r',
'a',
' ',
'd',
'a',
's',
'a',
's',
'l',
'k',
' ',
'a',
' ',
'f',
'd',
's' ]

Is this ok? Why it shows so many letters? How could it prove that my name exist in the text?


#2

You were intended to make your name uppercase.


#3

You did this exercise correctly. The reason you are getting a weird output is because anytime there is a letter "a" in text, the console will print that letter and the 4 after it (or whatever the length of myName is). You can probably see the result better if you change text to something like,

var text = "qeytpoqewarturwqpeoituweartur";

where there is no extra "a" besides in your name.


#4

I made uppercase now and nothing changed


#5

add (&& text[i+1] === "second letter") it will only find strings starting with the first 2 letters of your name.


#6

thanks. i changed for the uppercase and it works, but your way is good too for my other issue. Thanks!