.7 additional JS task exact match


#1

Hi I am trying to nail the additional exercise but I am having some issues and am unable to get the exact match function/variable to work correctly. maybe I have placed .match in an incorrect place.

var text= "Hi my nae is Thomas, \ this engine will find my name Thomas inside the text not Thooms"
var myName= "Thomas"
var hits= []
for (var i =0; i <text.length; i++){
    if (text[i] === "T"){
        for (var j=i; j<i+myName.length; j++){
            hits.push (text[j]);
        }
    }
}
var find = text.match (/Thomas/g);
    if (hits === 0){
        console.log ("Your name wasn't found!");
    }else {
        console.log(hits);
    }

#2

@koda82
Your missing semicolons at the end of your first 3 vars!


#3

Thanks for pointing that out I have added these but it still does help with the .match issue that I am trying to work out.
do you know if i am missing part of the setup for the .match? or is it an issue with the flag?


#4

Hi this part

if (hits === 0)

It should check if the length of hits is equal to 0 so you should add to hits .length


#5

Thanks for that code now looks like this
But I am still getting the same outcome. I want to only find my name "Thomas" within the array and not return "Tanker".

var text= "Hi my nae is Thomas, \ this engine will find my name Thomas inside the text not some other 6 lettered word like Tanker"
var myName= "Thomas";
var hits= [];
for (var i =0; i <text.length; i++){
    if (text[i] === "T"){
        for (var j=i; j<i+myName.length; j++){
            hits.push (text[j]);
        }
    }
}
var find = text.match (/Thomas/g);
    if (hits.length === 0){
        console.log ("Your name wasn't found!");
    }else {
        console.log(hits);
    }

#6

@koda82,

var text= "Hi my nae is Thomas, \ this engine will find my name Thomas inside the text not some other 6 lettered word like Tanker"
var myName= "Thomas";
var hits= [];
//initialize a character-collector
var charCollect;
for (var i =0; i <text.length; i++){
    if (text[i] === "T"){
        // be sure the charCollect is empty
        charCollect = "";
        for (var j=i; j<i+myName.length; j++){
            //hits.push (text[j]);
            // instead of =pushing= in Array
            //=concatenate the found characters
            charCollect += text[j];
        }
        // FOR loop ended all characters were collected
        // and concatenated the charCollect
        if ( charCollect == myName ) {
            console.log( "I found "+myName+" at position:\t"+i);
        }
    }
}

console.log( "== find with static ==================" );
var find = text.match (/Thomas/gi);
console.log( find );

// http://stackoverflow.com/questions/2295657/return-positions-of-a-regex-match-in-javascript

console.log( "== regex with static =================" );
var re = /Thomas/g,
    str = text;
while ((match = re.exec(str)) !== null) {
    console.log("match found at " + match.index);
}

// http://stackoverflow.com/questions/494035/how-do-you-pass-a-variable-to-a-regular-expression-javascript
//or using myName variable

console.log("== regex with variable myName =========");
str = text ;
var re = new RegExp(myName, "g");
while ((match = re.exec(str)) !== null) {
    console.log("match found at " + match.index);
}

#7

Thanks for the detailed explanation and the links :grinning:
I have been struggling with the loops exercise and so i decided to go back and re write the code so I could understand it better.

I also saw a post that you wrote in regards to how multiple loops work and it helped a lot.

Unfortunately I am beginning to log a strange output and I can not see where this output is coming from also the code passes as correct. I was hoping that you could take a look.

var text = "This string contains my name Thomas \ I want the engine to find may name Thomas not a six letter word like Tanker";

var myName = "Thomas";

var hits = [];

for ( i=0; i <text.length; i++ ){
    if (text[i]==="T"){
        for (var j = i; j <=i + myName.length; j++);
        hits.push(text[j]);
    }
};
if (hits.length === 0){
    console.log ("Your name wasn't found!");
}
else {
    console.log (hits);
}


#8

@koda82,
Your 2nd FOR-loop expression is ending with a semi-colon-;
causing the FOR-loop to iterate untill condition is false
and then the hits.push[j] is only processed once for every "T" found....
AND
as you are using a less equal operator, instead of a less than
operator, you will get PAST the actual text causing 1 undefined........


#9

Thanks for that, greatly appreciated.
upon review of the code I also found some more mistakes that I had made.
The process of going back and rechecking/fixing/rechecking has been incredibly helpful.:grinning: