Question on a line of code


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?


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


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


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


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


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


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


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.


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