Wednesday, May 15, 2013

J.S. Project: Search Text for Your Name



Today, we're going to write a short program that checks a block of text for my name. More specifically, it's going to check for the first letter of my name, then push (add) the number of characters equal to my name's length to an array. By inspecting the array, we'll be able to see if our name was mentioned.

/*jshint multistr:true */
text = "Blah blah blah blah blah blah blah blah Jenny blah \
blah blah blah blah blah blah Jenny blah blah blah blah \
blah blah blah blah blah blah blah blah blah Jenny";

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

// Look for "J" in the text
for(var i = 0; i < text.length; i++) {
    if (text[i] === "J) {
        // If we find it, add characters up to
        // the length of my name to the array
        for(var 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);
}

01 / 05 : Let's start by declaring our variables: text, myName, and hits. Since text could be quite long, try putting a blackslash (\) at the end of each line to make our string "wrap" to the next line of the editor. We can ignore the /*jshint multistr:true */ for now because all it does is tell the console to stop worrying about our use of backslash characters for wrapping long lines of text. Now set the following three variables:
1. text, and make a string containing some text
2. myName, and make a string containing just your name
3. hits, and make it an empty array
    HINT - make an empty array by setting your variable equal to [] (an array with nothing in it)

var text = "blah blah Jenny blah blah blah Jenny blah";
var myName = "Jenny";
var hits = [];

02 / 05 : Let's write our outer for loop. Below our existing code, create a for loop that starts at 0, continues until it reaches the end of text, and increments by 1 each time. (Meaning it will check each character in the string).
    HINT - Remember the syntax for a for loop looks like this:
        for(var i = 0; i < condition; i++) {
            // Do something !
        }
    In place of "condition", put the condition that will stop the loop from running. In this case, our  
    condition should be less text.length.

for(var i = 0; i < text.length; i++) {

}

03 / 05 : Nice! Now let's move on to the if statement. We'll want to place the if statement inside our for loop to make sure the program checks the if statement each time it moves forward through the loop. Essentially, the for loop is saying: "Hey program! Go through every letter in 'text'." The if statement will say: if you see something interesting, push that text into an array!" You can treat a string like an array of characters: For instance, you know that
        var myArray = ['hello', 'world'];
        myArray[0]; // equals 'hello'
But this also works on strings!
        var myName = 'Jenny';
        myName[0] // equals 'J'
Add your if statement in the body of your for loop and it should check to see whether the curent letter is equal to the first letter of your name.
    HINT - Remember, an if statement looks like thisis:
        if (some condition) {
            // Do something!
        }

    You can check the i th letter of text like so: \
    // Assuming your name starts with 'E'
    if (text[i] === 'E') {
        // Do something!
    }

for (var i = 0; i < text.length; i++){
    if (text[i] === 'J') {
 
    }
}

04 / 05 : Now time to add your second "for" loop. You're going to write it inside the body of your first if statement (between the if's {}s). This loop is going to make sure each character of your name gets pushed to the final array. The if statement says: "If we find the first letter of your name, start the second for loop!" This loop says: "I'm going to add characters to the array until I hit the length of the user's name." So if your name is 11 letters long, your loop should add 11 characters to hits if it ever sees the first letter of myName in text. We'll want to set our second loop's iterator to start at the first one, so it picks up where that one left off. If our first loop starts with
    for(var i = 0; // rest of loop setup
our second loop should be something like
    for(var j = i; // rest of loop setup
In the body of our loop, have our program use the .push() method of hits. Just like strings and arrays have a .length method, arrays have a .push() method that adds the thing between parentheses to the end of the array, for example:
    newArray = [];
    newArray.push('hello');
    newArray[0] // equals 'hello'
    HINT - the loop should stop when it hits the value of the first iterator plus the length of the myName
    variable.

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

05 / 05 : You've now got the engine of your search program running! It's going to:
    1. loop through the array
    2. compare each letter to the first letter of your name, and if it sees that letter:
    3. it will push that letter and all the letters that follow it to an array, stopping when the number of
        letters it pushes are equal to the number of letters in your name.
So under your existing code (and outside all your other loops!) Set up an if / else statement. If you don't have any hits, log "Your name wasn't found!" to the console. Otherwise, log the hits array to the console.

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

** Unfortunately, this system isn't perfect. For instance, if the paragraph contains both "Jenny" and "Jerry", we'll see this in our hits array:
['J', 'e', 'n', 'n', 'y', 'J', 'e', 'r', 'r', 'y']

1 comment:

  1. A great article so and a really elaborate, realistic and excellent analysis of the present and past eventualities.
    social name.

    ReplyDelete