Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript fine-tuning + find exact match for a name

Tags:

javascript

Is there a built in JavaScript string method that can help me fine tune this code to make sure it only finds exact matches for the name?

Here's my code.

/*jshint multistr:true */

var text = "Sid quick brown fox jumps over the lazy dog ";
var myName = "Sid";
var hits = [];

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

}
console.log(hits);
like image 644
sidph Avatar asked Feb 04 '26 17:02

sidph


2 Answers

So here's another solution, using function match(), but simpler:

/*jshint multistr:true */
var text = "Hey, how are you doing Sanda? My name is Emily.\
Nice to meet you Sada. Where does your name come from, Sana?\
is Sanda a slavic name?";
/*var myName = "Sanda";
hits = [];

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

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

var hits = text.match(/Sanda/g);

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

Syntax is var <someVar> = <textToBeSearched>.match(/pattern/modifiers). My modifier g is for global (searches the entire pattern).

More info here: http://www.w3schools.com/jsref/jsref_match.asp http://www.w3schools.com/js/js_regexp.asp

Cheers!

like image 112
Sanda Avatar answered Feb 06 '26 06:02

Sanda


The OP's example is from codecademy, so if anyone is searching for answers specifically related to Javascript: Lesson 6/7 this is what I came up with:

/*jshint multistr:true */
var text = "How are you \
doing Sabe? What are you \
up to Sabe? Can I watch \
Sabe?";

var myName = "Sabe";

var hits = [];

for (var i = 0; i < text.length; i++) {
    if (text[i] === 'D') {
        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);
}

 

I then decided, like the OP, to do the extra challenge which was creating a string that required an exact match. The codecademy javascript course is for complete newbies so the answer they were looking for was to be done using for loops and extensive if/else statements to give us practice.

The extended version from a learning curve point of view:

/*jshint multistr:true */
var text = "The video provides a powerful way to help you prove \
your point. When you click Online video, you can paste in the \
embed code for the video you want to add. You can also type a \
keyword to search online for the video that best fits your document.";

var theWord = "video";
var hits = [];

for (var i = 0; i < text.length; i++) {
    if (theWord === text.substr(i,theWord.length)) {
         hits.push(i);
         i += theWord.length-1; 
    }
}

if (hits.length) {
    for (i = 0; i < hits.length; i++) {
        console.log(hits[i],text.substr(hits[i],theWord.length));
    }

} else {
    console.log("No matches found.");
}

 

Then there is match() mentioned by @Sanda which is good to know for real life instances:

/*jshint multistr:true */

var text = "The video provides a powerful way to help you prove \
your point. When you click Online video, you can paste in the \
embed code for the video you want to add. You can also type a \
keyword to search online for the video that best fits your document.";

var hits = text.match(/video/g);


if ( hits.length === 0) {
    console.log("No matches found.");
} else {

    console.log(hits);
}

 

I hope this helps the codecademy folk!

like image 36
phoenixlaef Avatar answered Feb 06 '26 05:02

phoenixlaef