5. Your second "for" loop. Not understanding the second loop


#1

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

I've read through the great explanations that others have written on the second loops and I still cannot get it. I apologize, I hope you can be patient with me.

My first question is, why is: if (text[i] === "T") written with an array around i and not brackets?

My second question is regarding the second loop. I don't understand why we are making j equal i and why text[i] has to be less than not just myName.length but myName.length plus i.

I understand that the first loop is looking for the first letter of my name and it starts at zero because that is what the string starts at and it checks the entire string using the first loop. I understand that the first if statement is telling it to run the following first for loop if R is found. And this is where I get lost.

I hope I've been able to state what I understand and don't. I realize it is hard to grasp concepts from just reading it.

Thank you so much.

/*jshint multistr:true */
var text = "I'm testing to see if my name, Rin, is here";
var myName = "Rin";
var hits = [];
for (var i = 0; i < text.length; i++) {
if (text[i] === "R") {
    for (var j = i; text[i] < myName.length + i; j++) 
}
};


JavaScript Unit 3 Lesson 6: Your second "for" loop - PLEASE EXPLAIN
#2

Pff... i will try to make it shorter then the other one you have read. so your first question is, why:

if (text[i] === "T")

and not:

if (text(i) === "T")

do i understand that correctly? Because under the hood, a string is actually a array. during the loop, i is a number. By doing text[0] we get the first letter of the text, text[i] will get the letter at index i. doing text(i) would be a function call, we don't have a function (are function even covered yet?) called text. Bad idea

take a look at this understandable code:

var text = "I'm testing to see if my name, Rin, is here";
var myName = "Rin";
var hits = [];
for (var i = 0; i < text.length; i++) {
if (text[i] === "R") {
    console.log(i)
    console.log(text[i])
}
};

i am asking this program to log the variable i to the console, and then then i use i to also log the letter at index 31 to the console. Which happens to be the letter R

So far so good? So now we want to store the letters at index 31 (letter: R), index 32 (letter: i) and index 33 (letter: n) into our hits array (we declared an empty array hits: var hits = []; in the beginning of your code). So, we want to give j to loop over index 31, 32 and 33. If you remember from a moment ago, i was 31. So that is why we do: j = i, now j knows where the start of your name is.

Now, we want to loop over your name. But we also want the loop to stop. It so happens that i + myName.length is 34. (i is 31), which ideal, since your name are at indexes 31,32 and 33. So, j < i + myName.lenght (which in the first run of the loop would be: 31 < 31 + 3, which can be rewritten as:3i < 34`, which ideal, since that covers your name perfectly:

var text = "I'm testing to see if my name, Rin, is here";
var myName = "Rin";
var hits = [];
for (var i = 0; i < text.length; i++) {
if (text[i] === "R") {
    for (j = i; j < i + myName.length; j++){
        console.log(j)
        console.log(text[j])
    }
}
};

Search Name for Your Text 5. Your second for loop
Not understanding "j < (myName.length + i" part of for loop
Give a good explanation for this code please
Search text for your name: Second for loop
Your if Statement- Page keeps freezing
#3

stetim94, thank you so much for your explanation!

I was so focused on learning the for loops that I forgot about the function() that I just learned last lesson.

I do understand what you are saying now. For some reason I could not wrap my head around why j was there, but having you explain it as j = i so that the second loop knows where the start of your name is...that made it click for me. The first loop is going through the entire text string to find the 'R' and the second loop is finding the rest of your name since the first loop found where it began.

Thank you so much again!


#4

Seems you understand it :slight_smile: It was just the realization what j = i does. And why it is needed


#5

I dont understand what j is supposed to be


#6

Well, then you call it differently. j is just a variable name the exercise gave you. If you need more help, please create your own topic :slight_smile: