5. Your Second "for" Loop


#1

I am totally confused with this part of the exercise. The directions say "This loop will make sure each character of your name gets pushed to the final array." What do they mean by "pushed to the final array"?

And also this below, can someone explain this plainly? I don't understand what they are trying to tell me. What does hit do since I have it as the variable name and it is an empty array right now...

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."


#2

@goldiefish,

In the first for-loop you are running over the text
â—¦character by character using text[i] as character-value, as long as text.length
â—¦comparing it to "D" in a IF-statement
â—¦- then in the second for-loop you preserve the i-variable by making the x-variable to work with.
â—¦- now using x you want to go over the text from position i upontill i + nameLength
â—¦- so you have to change the condition of the second for-loop to
â—¦- x < i + nameLength (and do not use the less-equal-operator)

Then at the end you might want to see the result of the hits-array
console.log( hits );

References::

google search
javascript push array site:developer.mozilla.org
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push
(also look in left-hand collumn Methods.... )

javascript push array site:stackoverflow.com
= http://stackoverflow.com/questions/351409/appending-to-array

++++++++++++++++++++++++++++++++++++++++++++
++++ given answers on search myName

http://www.codecademy.com/forum_questions/547ba85b631fe9ad87009a9b
http://www.codecademy.com/forum_questions/5485cf6f95e378ba93002723


5/7 for loop?
#3

@goldiefish,

++++++++ search text for your name
+++++++++++++++++++++++++++++++++++
paste-in following code....then submit....
you will get an reference-error..no problem !!
analyze the console-output and the code provided to get a better insight...
the \n is new-line
the \t is a tab

var text = "alex is the alex";
var myName = "alex";
var hits = [];
console.log("\n\nThe variable myName =" + myName);
console.log(
"Taking the first-letter of myName[0] = " + myName[0] );
console.log(
"Taking the 2nd -letter of myName[1] = " + myName[1] );
console.log(
"Taking the 3rd -letter of myName[2] = " + myName[2] );
console.log(
"Taking the 4th -letter of myName[3] = " + myName[3] );

console.log("\n\nLooping over the whole of text::");
for (var i = 0; i < text.length; i++) {
console.log(
"i=",i," letter of text at pos",i," is "+"<"+text[i]+">"
);
}

console.log("\n\nFinding the position's"+
"\n\tof the 1st-myName-letter in text");
for (var i = 0; i < text.length; i++) {
if ( text[i] === myName[0] ) {
console.log(
"i=",i," letter of text at pos",i," is "+"<"+text[i]+">");
}
}

console.log("\n\n-Now find-pos AND "+
"\n-from pos, gather letters"+
"\n-untill pos+myName.length");

console.log('===================');
for (var i = 0; i < text.length; i++) {
if ( text[i] === myName[0] ) {
console.log(
"i=",i," letter of text at pos",i," is "+"<"+text[i]+">");

for(var j=i; j<(myName.length+i); j++){
console.log(
">j=",j," letter of text at pos",j," is "+"<"+text[j]+">");
hits.push(text[j]);
}

console.log('===================');
}
}
console.log( hits );
error

References:

google search
javascript push array site:developer.mozilla.org
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push

javascript push array site:stackoverflow.com
= http://stackoverflow.com/questions/351409/appending-to-array

www.crockford.com
http://javascript.crockford.com/survey.html
http://javascript.crockford.com/code.html


#4

In layman's terms:
This code scans through a given text, searches for the letters in the variable "name" and adds them into your empty array.

The exercise asks you to write out some sample text that contains at least one appearance of your name. Ex) "My name is Greg, and Greg likes pudding."
It asks you to give your name. Ex) "Greg"
And it asks you to create an empty array (or list, if you will) called hits.

The for loop and additional code does the following:
1) It scans through your sample text for the first letter of your name.
2) After it finds the first letter of your name in the sample text, it will add that letter into hits.
3) Then it will continue to see if "r", "e", and "g" are in the sample text and add them into hits if they are.
4) Each time "Greg" appears in the sample text (in this case, twice), "G", "r", "e", "g", will be added to hits.
5) At the end of all this, my hits should contain:
hits = ["G", "r", "e", "g", "G", "r", "e", "g"]

Hope this helps.