The Search Text for Your Name exercise is confusing because


#1

https://www.codecademy.com/en/courses/javascript-beginner-en-XEDZA/0/5?curriculum_id=506324b3a7dffd00020bf661

It's confusing because it does not specify in the beggining of making this program that it does not search for the entire word. It will only find the first letter of your name: myName[0], puts that and the next letters based on however many letters are in your name: myName.length into the empty array hits = [ ]

So if you did this:

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

You would get that:

[ 'a', 'm', 'o', 's', 'a', 'n', 'd', 'y', 'a', 'm', 'o', 's' ]

even though the you were searching for amos and not andy!!!


#2

Yes. They inform about this in the last exercise, which is a total stupidity.

Now, as we mentioned, this system isn't perfect. For instance, if the paragraph contains both "Eric" and "Eddie", we'll see this in our hits array:
['E','r','i','c','E','d','d','i','e'];


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.