Search text for Your Name; Any ideas for fine-tune improvements?


#1


"Think about how you might fine-tune this program to make sure it only
finds exact matches for your name. Search the Internet to see if there
are any built-in JavaScript string methods that can help!"


Managed to have the output display the desired string, but not all instances of it... Any suggestions?
Much appreciated!

var text = "Nick jam jam Nidge jam jam Nick jam\
jam Nick jam jim jam Nick jam NIck";
var textArray = text.split("");
var myName = "Nick";
var myNameArray = myName.split("");
var hits = [];

for (i = 0; i < textArray.length; i++){
    if (textArray[i] === myNameArray[i]){
        for (j = i; j < (myNameArray[i].length + i); j++){
       hits.push(textArray[j]);  
        }
    }
}

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

else {
    console.log(hits.join(" "));
}


#2

The output that this code produces is:

N i c k

But I'm looking to output all corresponding values.

Thanks.


#3

Start by finding out if they're all pushed, and why they would be pushed.


#4

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