Victory : fine tuning


#1

Ok, so here is my code for the "fine tuning" section. Any other better suggestions??

    var text = "hika dummy dummy dummy dummy Himanshu \
    dummy more dummy dum dum Himanshu this is how \
    it's going to be, Himanshu himanshu looking good for now,Hika";

    var matchMyName = text.match(/Himanshu/gi);
    var hits = [];
    // Condition to check if search string exists
        if (matchMyName === null) {
            console.log ("Your name was not found");
        }
    // If search string exists, run the for loop
        else {
                for (var i = 0; i < matchMyName.length; i++) {
                    matchMyName[i] && hits.push(matchMyName[i]);
                // console.log (matchMyName[i].split(""));
           };
    // Output array as one string
        var hitsConcate = hits.join("");
    // Spit an array of each character of the search string
        var searchResult = hitsConcate.split("");
    };
           
    console.log (searchResult);

#2

Hey blogrockstar, this is what I did -
I used an existing elegant javascript function called ".match":

/* my text to search through /*
var text = "simple Michael simple simple simple \
simple simple simple simple simple Michael \
simple simple simple simple simple simple simple \
Michael simple simple simple simple simple";

var myName = "Michael";

var hits = [];

/*creating a new variable called "textMatch". This is a function which goes through my text and looks for anything that matches "Michael". The g means that after it finds one match, it continues looking for more (global). See w3 schools for more details on this: http://www.w3schools.com/jsref/jsref_match.asp /*
var textMatch = text.match(/Michael/g);

/* if .match function finds no name, it returns "null". So if the programme gets a null, it tells the user their name isn't here /*
if (textMatch === null) {
console.log("Your name ain't here mate");
}
/* if the programme finds my name, it prints each iteration out, and also tells me how many times./*
else {
console.log(textMatch)
console.log("Your name appears " + textMatch.length + " times")
}

Hope that helps


#3

its good but it can give me wrong answer too.
suppose someone name is Michael2 and i want to know how many times Michael is here
like

var text ="simple Michael simple simple simple \
simple simple Michael2 simple simple simple Michael \
simple simple simple Michael2 simple simple simple simple \
Michael simple simple simple Michael2 simple simple";

it'll tell me Your name appears 6 times but my name is only 3 times

@boardslayer56318


#4

You are right, I did not notice that. I fixed it with RegExp -

/*jshint multistr:true */
var text = "hika dummy dummy dummy dummy Himanshu2 \
dummy more dummy dum dum Himanshuu this is how \
it's going to be, Himanshu himanshuhimanshu looking good for now,Hika";

var matchMyName = text.match(/\b(Himanshu)\b/gi);
var hits = [];
// Condition to check if search string exists
    if (matchMyName === null) {
        console.log ("Your name was not found");
    }
// If search string exists, run the for loop
    else {
            for (var i = 0; i < matchMyName.length; i++) {
                matchMyName[i] && hits.push(matchMyName[i]);
            // console.log (matchMyName[i].split(""));
       };
// Output array as one string
    var hitsConcate = hits.join("");
// Spit an array of each character of the search string
    var searchResult = hitsConcate.split("");
    };
       
console.log (searchResult);

#5

Yes good point.

You could try "text.match(/Michael /g)"

Not sure if that would work or not...


#6

it will not work.. Using RegExp fixes it as I have mentioned in the code above..