Codecademy: JavaScript “Search Text for Your Name” 7/7 – Victory!

My Sunday nights are even more crazier.

“Log it!” 6/7 code:

var text = "Adam said: Who the hell is this, paging me at 5:45 in the morning \
crack of dawning. Wipe the cold out of my eye, see who's this paging \
me, and why. My name is Biggie Adam Smalls.";

var myName = "Adam";

var hits = [];

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

Task: This system isn’t perfect. For instance, if the paragraph contains both “Adam” and “Aaron”, we’ll see this in our “hits” array:

Think about how you might fine-tune this program to make sure it only finds exact matches for your name. Search the Internet to see if there are any built-in JavaScript string methods that can help!

substring() is your friend. Why? Because it creates a ‘temporary string’ of characters within a range to let you compare it against your name. The first parameter is the starting point of the ‘temporary string’ and the second is the end.

/*jshint multistr:true */
var text = "Adam said: Who the hell is this, paging me at 5:45 in the morning \
crack of dawning. Wipe the cold out of my eye, see who's this paging \
me, and why. My name is not Aaron, it is Adam.";

var myName = "Adam";

var hits = [];

for (var i = 0; i < text.length; i++) {
    if (text.substring(i, myName.length + i) === myName) {
        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);
}

4 Comments:

  1. Hello, Adam! Thank you for the your solution. Could you explain why we should add “i” in line 12 “text.substring(i, myName.length + i)”, not just “text.substring(i, myName.length)”.

    • Hi Alexander,

      No problem.

      The net result is that it successfully searches the entire text string from start to finish moving along 1 character at a time per iteration of the for loop.

      The technical explanation is that substring() takes two arguments; substring(start, end). Both “start” and “end” are values which index a given string, i.e. if the “start” value was 0 it would extract from the 1st character. As the “end” value was myName.length, it would extract up to the 4th character. If there was no +i, then the end value would constantly be 4; the “end” value would not move along 1 character at a time per iteration of the for loop, it would be fixed on 4. So by incrementing its value, we can move along nicely at the same ‘character-width’ as myName.

      Make sense?

  2. Thank you, Adam! I got you now. Have you thought of other methods to make it work before using substring?

    • I can’t remember what I attempted before I stumbled across substring, sorry. I can imagine though that if I wasn’t using a function (or “method”), then it would have been an ugly attempt of multiple loops and variables.

      Half the battle sometimes I find with programming is finding what functions are already out there, and having a concise description of them to see if they apply to your requirement or not.

Leave a Reply

Your email address will not be published. Required fields are marked *