How to search text for my name, not just the starting letter and following letters


#1



My code works fine and I passed the lesson. What I am curious about is how do I go about making the code only search for my name instead of the other "D"s I placed in the text.


/*jshint multistr:true */

var text ="My name is David.  It is nice having the name David! \
I am practicing wrapping text onto the next line, because my name is David.  \
Dr. Dolittle.";
var myName = "David"
var hits = [];

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

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


#2

you could build another if statement to also check for the last letter (text[i] + myName.length - 1), you could also use the and operator (&&), if you know how to

you can also build another to loop to verify your name has been found and all letters match


#3

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