Search text question


#1

hello
i have a question .. i wrote this code

 /*jshint multistr:true */
text= "Aa Bb Cc Dd Ee Ff Gg Hh Ii Jj Kk Ll Mm Nn Oo Pp Qq Rr Ss Tt Uu Vv Xx Yy Zz";
myName= "AMIR";
hits=[];
for (i=0; i<=text.length; i++){
    if(text[i]===myName[0]){
        for(x=i; x<(myName.length+i); x++)
        {hits.push(myName[x])
        }
    }
}
if(hits.length===0){
    console.log("Your name wasn't found!");
}
else{
    console.log(hits);
}

and i got the right answer i need but i can't understand how
it suppose to write myName[0] and the letters that follow but instead it looked for every letter i need and then posted it
any one please can explain how ??
thanks


#2

the answer change when i change the name but still can't completely understand how ,, i want to know how to make the code that search for every letter no matter what is the name or the sentence in myName


#3

your name should be inside the string stored in text, if you just want to find all the letters you will have to write the program differently


#4

ok here

text= "s s Aa Bb Cc Dd Ee Ff Gg Hh AMIR Ii Jj Kk Ll Mm Nn Oo Pp Qq Rr Ss Tt Uu Vv Xx Yy Zz";
myName= "AMIR";
hits=[];
for (i=0; i<=text.length; i++){
    if(text[i]===myName[0]){
        for(x=i; x<(myName.length+i); x++)
        {hits.push(myName[x])
        }
    }
}
if(hits.length===0){
    console.log("Your name wasn't found!");
}
else{
    console.log(hits);
}

the result i get from this is
[ undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined ]
how ??


#5

the second for loop serves as purpose to find your name in text, then extract them from text and push them into the hits array:

hits.push(myName[x])

you use myName here, that should be text


#6

i didn't get what you mean


#7

you want to find all the instances of your name in the string stored in text, so you want to push the letters from text into your hits array, not myName


#8

you mean that should be the hits code

hits.push(text[x])

??


#9

of course, you want to find all instances of your name in text, so that is what i would push


#10

that's the result
[ 'A', 'a', ' ', 'B' ]


#11

then you changed more, it also finds AMIR:

text= "s s Aa Bb Cc Dd Ee Ff Gg Hh AMIR Ii Jj Kk Ll Mm Nn Oo Pp Qq Rr Ss Tt Uu Vv Xx Yy Zz";
myName= "AMIR";
hits=[];
for (i=0; i<=text.length; i++){
    if(text[i]===myName[0]){
        for(x=i; x<(myName.length+i); x++)
        {hits.push(text[x])
        }
    }
}
if(hits.length===0){
    console.log("Your name wasn't found!");
}
else{
    console.log(hits);
}

#12

but find the letters i wrote before it
[ 'A', 'a', ' ', 'B', 'A', 'M', 'I', 'R' ]
i want to be AMIR only


#14

wrong last reply , sorry


#15

you could use substring() or also check for the last character by adding another if statement inside your current if statement, something like this:

if (myName[myName.length - 1] == text[i+myName.length-1])

#16

that works thank you


#17

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