6 Log it! - Query regarding output


#1



Hey - OK so, I'm passing this section but I wanted to ask one thing;

The programme is outputting :
[ 'A',
'e',
'n',
'e',
'a',
'n',
' ',
'A',
'n',
't',
'h',
'o',
'n',
'y',
'A',
'n',
't',
'h',
'o',
'n',
'y' ]

I get that it is searching for "A" in the text, but is it correct for it to be returning other words and not just my name?


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


/*jshint multistr:true */
var text = "In diam diam, molestie Devon nec ullamcorper id, condimentum \
id mauris. Sed lacinia libero Devon vitae massa hendrerit vitae porttitor \
mauris porta. Praesent Devon dapibus vestibulum semper. Aenean posuere \
suscipit accumsan. Phasellus ac arcu Anthony Devon arcu. Phasellus sodales \
venenatis mollis. Fusce dignissim Devon mollis tortor sed fermentum. \
Pellentesque egestas, Devon mi accumsan  Anthony tempor, justo felis.";
var myName = "Anthony";
var hits = [];

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


// Below is the code from the example which ran successfully when I put it in place of mine but I can't see the difference other than the letter "E". Help?
for(var i = 0; i < text.length; i++) {
	if (text[i] === "E") {
		// 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);
}


#3

Unfortunately... yes. This is one of the worst codecademy sections.

Quote from the last lesson of this section:

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']


#4

This is a starting point, searching for any text that begins with a letter and is of some fixed length, such as a name. We are free to improve upon the code once we reach the last lesson but we should not ignore the concepts being expressed here. Matching and nested loops, among them. The Array.push() method is introduced here for the first time in the track, as well.