5/7 What does this mean?


#1

Hello,
I passed this section of the JavaScript coding but I truly don't understand what this part of the code means or why its there: "(for (var j = i; j < (i + myName.length); j++)". Specifically pointing out this part "(i + myName.length)". What does that mean? Please explan it as simple as possible or as best as you can. I truly just want to understand what it means.


#2

The number of times this has been explained in the fourms is practically innumerable, seriously. You could almost close your eyes and hit a post on the subject. Just the same, let's have one more go at it...

i is the outer loop iterator whose present value is the index number of the current first letter match.

text[i] === myName[0]

To iterate over myName inside text from that position, we need to set a starting point, i which we give to j, and an ending point, i + myName.length. i is the offset to the first character in myName as matched in text at position [i]. We need to include this offset in determining the end position, else our inner loop might not even run.

Please peruse the Q&A for other questions like this. A little searching will bring up all kinds of explanations, all pretty close to the above. When you find the one that best answers this question, please post a link back here so we can bookmark it. Thank you.


#3

Let me try and break it down for you.

my name is Chris in my code.

Remember in a string of text the loop counts. It counts each character starting at 1, 2, 3...

First loop says look through all of the characters and IF you find C, stop and run the next code block.

At this point it found the start of my name but needs instructions to push all of the characters and ONLY the characters in my name to the array. Remember it's not looking for h, r, i, s. It's only looking at the count in the text string.

In my example, C was the 6th count in my text string.

We found C. Now let's check to see how many characters are in the name Chris. It's 5.

So now we found the first C which is the 6th character. We know my name is 5 characters long. So now we want to push 5 characters to the array starting at the 6th string count. We want to push character counts 6 (C), 7 (h), 8 (r), 9 (i), 10 (s). But how we do we tell the loop to stop pushing characters to the array. It doesn't know that your name ends with an s.

for (var j = i; j < (myName.length + i); j++) Could read as we know that i is 6 because it stopped on the 6 character which is C. We want to start our second loop off on the C character or 6th spot. So we set j as i.

(var j = 6; 6 < (5 + 6); 6++) =
( 6; 6 < 11); 6 ++)
Our setup starts at 6 or C;
Our condition is we know Chris is 5 characters. We are starting at 6th spot. We want to find 5 characters. 6 + 5 = 11. We want to stop after we have found all 5 characters of Chris. That's how we know we have the name.
Then add one to 6 and so on in each loop until it reaches 11 and stop.
On each loop it's going to push that number of the text string to the array.

The numbers will be different depending on where your name falls in the text string count.


#4

Thank you very much vistringsamri for going into extensive detail to explain to me what that meant. I really appreciate it and I couldn't ask for a better one. I know sometimes people don't look into code to hard but I for me I feel that I have to understand why some certain code there in the mix. Thank you again .


#5

Thats the truly difficult part. I read and looked at this section of code, wrote out what was happening for a few hours until it all popped and made sense. When it did, it opened up a lot of other things. If you still don't understand fully, let me know and I'll try to rephrase it better.


#7

This is a great explanation, thanks for this vistringsamri.


#8

THIS is how you explain a concept. Well done!! There are a ton of brilliant people who post on these forums but many lack the ability to explain on a proper level using terms and examples that non-weathered coders can make sense of. Thanks to your post I haven't wasted valuable time attempting to translate scholarly posts. I understand the idea and I can build on it with full understanding.