Question on a line of code


#1

var text = "Hello, my name is Clover."
var myName = "Clover"
var hits =[]

for (var i = 0; i < text.length; i++){
if(text[i] === "C"){

    for (var j = i; j < i + myName.length; j++){
        if(text[j] === myName[j-i]){

        hits.push(text[i]) }}
    }

}

so I saw this code and I do not understand why the "if(text[j] === myName[j-I]) {" is in there. I just came across this code and wasn't sure why it is needed. I don't have that in my code and it worked fine so I'm not sure if it is needed or if it's not. Maybe my code is just not registering right?


#2

It partial prevents similair name (Micheal, Mike) to be pushed to the hits array


#3

alright I thought that's what it was for but I wasn't completely sure. thank you for the clarification


#4

but it doesn't work completely, Mi of Mike will still be pushed, even while the name you look for is Micheal


#5

so basically using that line of code would technically be wrong ?


#6

No, technically not. The program is not perfect (it can't distinguish between Mike and Micheal for example, this line makes it slightly better, but still not perfect


#7

ohh ok. understandable to make it better you would need to refine It to make it find Michael instead of mike? sorta?


#8

This depends which name you look for, michel/mike was just an example, but you could try to refine it, so it only finds the name you want, and not similair names.


#9

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