Fine-Tuning Name Search


#1


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


I'm trying to figure out how I can fine tune this program so that it only finds the defined name and not ones that might start with the same letter. Anyone actually gone through with this and have a solution?


var text = "Accent, Multiple, Ian, Civics, Classroom, Ian, Bathroom, Elephants, Ian, Homeroom";
var myName = "Ian";
var hits = [];
var nameCheck;

for(var i = 0; i < text.length; i ++){
    if(text[i] === myName[0]){
        for(var j = i; j < i + myName.length; j ++){
            hits.push(text[j]);
        }
        i = j;
    }
}

if(hits.length === 0){
    console.log("Your name wasn't found!");
}
else{
    console.log(hits);
}


#2

This part of code...

i = j ?
Try removing it!


#3

The i=j is how I have my code set up so that the first for loop knows where
to pick up. I'm trying to get it so that it only picks up my name. Say the
"text" variable has a string "Igloo" in it as well, the program would
currently push 'I' 'g' and 'l' into the hits array. I want my code to be
able to realize that it has picked up a string that isn't my name. Thanks
though!


#4

One thing...I see there is no use of... var nameCheck;


#5

Oh yeah I know haha, for some reason I didn't implement the code as a
function


#6

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