Adding second "for loop" to search for name ...?


#1



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


I know this step has been discussed a lot, but my brain is literally melting and I would really appreciate if someone could just help me talk it through. I've done the first "for loop" and that makes sense to me - finds any part of the text that starts with the letter L. Then, I'm pretty sure I understand how to make that loop stop when it hits the length of my name. But the example shows var j = i ... EXCUSE me, what is j?? When was j ever introduced? What does j mean? Is j part of regular syntax that we should know, or is j something that only works in this example?

And finally, these instructions come STRAIGHT out of left field:

"Finally, in the body of your loop, have your 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'
"

Where does this code get entered? Into the same "for loop"? Does it start on a new line? How does the computer know what newArray is? Is this supposed to be the "hits" array, or something completely new? Do we need to introduce it as var = newArray? There is just so little context about what this part means, I feel totally lost. And the instructions only tell us to create the second "for loop", and don't mention anything about using this new code. Here is what I've entered in my code so far:


/*jshint multistr:true */
var text = "Hey, how are you doing? My name is Lily.";
var myName = "Lily";
var hits = [];

for (var i=0; i < text.length; i++) {
    if (text[i] === 'L') {
        for(var i = 0; i < myName.length; i++)
        }
        newArray = [];
        newArray.push('hello');
        newArray[0];   // equals 'hello'
    }


Not understanding "j < (myName.length + i" part of for loop
Search Name for Your Text 5. Your second for loop
JavaScript Unit 3 Lesson 6: Your second "for" loop - PLEASE EXPLAIN
#2

This line:

You really declare j there in the for loop. Look at your first loop:

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

you declare i inside your for loop, and you the same here:

for (var j = i; /* rest of the code */

but instead of assign a value directly (0, like the first for loop), you give it the value which stored in your variable i. For the exercise it is called j, but you could also called it something else. So where does it come from? You created the variable j

It is important to realize that you use the second for loop to loop over Lily. Okay, lets look at a simple example (not part of the exercise):

myName = "Lily"
for (letters = 0; letters < myName.length; letters++){
   console.log(letters);
}

We make a variable with your name, then inside the for loop we create a variable called letters, which we then print to the console, agree that this will show the letters on a new line? Which will look like this:

'L'
'i'
'l'
'y'

And this is where it gets tricky, we want to do this thing for our hits, lets upgrade the code:

myName = "Lily"
hits = []
for (letters = 0; letters < myName.length; letters++){
   hits.push(letters);
}
console.log(hits);

we made an array, but instead of printing the letters directly to the console, we pushed them in the array, using push(), then we printed the array to the console, which will look something like this:

['L','i','l','y']

(feel free to run the code samples i put here, to increase your understanding, you can always add more console.log statements to see the working)

Right, and this is where it gets complicated. Because now we only want to push something into our hits array, if it is your name. But, we don't know at which indexes this letters are. However, the moment your if statement is true:

if (text[i] === 'L')

i will hold the index (the number the letter is positioned at), and we can access letters at the index by doing text[i], we can see this in action:

var text = "Hey, how are you doing? My name is Lily.";
var myName = "Lily";
for (var i=0; i < text.length; i++) {
    if (text[i] === 'L') {
        console.log(i)
        console.log(text[i])
   }
}

Okay, so i is 35. Agree with me that index 36 (you can see this by running text[36]) is the letter i, at index 37 is the letter l, and at index 38 is the letter i. Also agree with me that 35 + myName.length is 39. Wow, that is ideal, those are the numbers we need. We need the numbers smaller then 39, which so happend to i + myName.length, we can write a for loop for it:

for (var j = i; j < myName.length + i; j++){
   console.log(j)
   console.log(text[j])
}

now j will be, 35 in the first run (remember: the variable i is 35), so the variable j will now go to 36, 37, and 38 (which so happen to be the indexes at which your name is located), i added some console.log statements, so you can see what the code is doing

I hope this helps


5. Your second "for" loop help
#3

Wow, this makes so much more sense now. Thank you for the explanations!!!

Everything works in my console until we get to the second loop. I get i = 35, I get text[i] = L, but once I add in the second loop nothing logs to the console and the error message keeps saying that my second for loop needs to end "when it reaches its current point in the string + myName.length", which it looks like my for loop HAS that parameter. So I'm not sure what's wrong with it. Is there something I'm still missing?

This is my updated code:

var text = "Hey, how are you doing? My name is Lily.";
var myName = "Lily";
var hits = [];

for (var i=0; i < text.length; i++){
        if (text[i] === 'L'){
            for(var j = i; j < myName.length; j++){
                hits.push('j');
                }
            }
    }
console.log(hits);

#4

Good it makes more sense, this must by far the most extensive answer i written about the second for loop.

for(var j = i; j < myName.length; j++)

In the first run of the loop, j is 35. myName.length is 4. 35 < 4, which is false, so the loop ends. It seems i made a mistake in my explanation. it should be `j < myName.length + i`

Why should this be? I explained that here:

Also, you are currently pushing the letter j into your hits array, what do you think this should be? maybe see what console.log(j) and console.log(text[j]) give as output.