Think about how you might fine-tune this program to make sure it only finds exact matches for your name


#1

I just surf on the internet but I don't find solution for it. I have no idea how to change the code to return only my name when push the submit button.
This is the code:
/*jshint multistr:true */
var text = "You are my guest. You can visit my house but you must not touch anything. My name is Mihai and I love sport. blah blah blah Mihai is good with computers.";
var myName = "Mihai";
var hits = [];
for(i = 0; i < text.length; i++) {
if(text[i] === "M") {
for(j = i; j < (myName.length + i); j++) {
hits.push(text[j]);
}
}
}
if(hits.length === 0) {
console.log("Your name wasn't found!");
}
else {
console.log(hits);
}


#2

Bump. I have no idea how to go about googling this solution.


#3

Here is the example of how I did it. I've used pure logic without any additional string methods. You can also use it for searching not only one word but whole phrase as well. It returns the amount of matches in text and it's postion.

var text = "You are my guest. You can visit my house but you must not touch anything. My name is Mihai and I love sport. blah blah blah Mihai is good with computers.";

var myName="Mihai";
var hits=[];
var flag=0

for (i=0; i<=text.length;i++)
{
    if (text[i]===myName[0])
    {
        for (k=1; k<myName.length; k++)
        {
            if(text[i+k]!==myName[k])
            {
                  flag++;
            }
        }
        if (flag===0)
        {
            hits.push(i)
        }
        flag=0;
    }
}

if (hits.length===0)
{
    console.log("Your name wasn't found!");
}
else
{
    console.log("Text \""+myName+"\" was found "+hits.length+" times. Positions: "+hits );
}

#4

This won't work because it keeps pushing before it determines if the name is a exact match. For example, "Eric" and "Erin" will still be picked up. This is what I've come up with:

/*jshint multistr:true */

var text = "blah blah blah blah Gillian blah blah blah blah \
blah blah blah blah Gillian Grace blah blah yadda yadda yadda";

var myName = "Gillian";
var hits = [];

for (var i=0; i < text.length; i++) {
if (text[i] === myName[0]) {
// check if the string matches myName
for (var k=i+1; k < (myName.length + i); k++) {
if (text[k] === myName[(k-i)]) {
var match = true;
}
else {
var match = false;
}
}
if (match === true) {
for (var j=i; j < (myName.length + i); j++) {
hits.push(text[j]);
}
}
}
}

if (hits.length === 0) {
console.log("We can't find your name.");
}
else {
console.log(hits);
}


#5

This is my method. As soon as I find the first matching character, I then look for a match on myName against the substring of text.

/*jshint multistr:true */
var text = "Hey, how are you \
doing? My name is Santiago.  I once knew a man named Santiago. San Diego \
supposed to be a nice city.";
var myName = "Santiago";
var hits = 0;

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

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

#6
/*jshint multistr:true */

var text = "The trees are in their autumn beauty,\
The woodland paths are dry,\
Under the October twilight the water\
Mirrors a still sky;\
Upon the brimming water among the Erwin\
Are nine and fifty swans.\
The nineteenth Autumn has come upon Erwin\
\
Since I first made my count;\
I saw, before I had well finished,\
All suddenly mount\
And scatter wheeling in great broken rings\
Upon their Erwin wings.\
\
I have looked upon those brilliant Erwin,\
And now my heart is sore.\
All’s changed since I, hearing at twilight,\
The first time on this shore,\
The bell-beat of Erwin wings above my head,\
Trod with a lighter tread.\
\
Unwearied still, lover by lover,\
They paddle in the cold,\
Companionable streams or climb the air;\
Their hearts have not grown old;\
Passion or conquest, wander where they will,\
Attend upon them still.\
\
But now Erwin drifts on the still water\
Mysterious, beautiful;\
Among what rushes will they build,\
By what lake’s edge or pool\
Delight men’s eyes, when I awake some day\
To find the Erwin have flown away?";

var myName = "Erwin";
var hits = [];

for(var x = 0; x <= text.length;x++)
{
    if(text[x] === myName[0])
    {
        var newArray = [];
        var newString;
       for(var y = x; y < x + myName.length; y++)
       {
           newArray.push(text[y]);
       }
        
        newString = newArray.join("");
        
        if(newString === myName)
        {
            hits.push(newString);
        }
        
    }
    
}

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


#7

I added a new array variable to hold the letters in the name and a new string variable to hold the array when it gets joined together. this in return allows me to check to see if the name it found in the text was actually my name.