Can't follow the logic in the for loop


#1

I don't understand how this works:
for(var j = i; j < (myName.length + i); j++) {
hits.push(text[j]);
}

As far as I can tell, if j = i, for any given value of i, j will never be greater than i + myName.length (in this case, 4), because isn't that asking if j is greater than itself + 4?

Can someone please explain the logic of this? I can't get my head around it. :confused:


#2

Me neither. So, is there anyone who wish to spare time to enlightens us or, actually, lots of us don't understand?


#3

Hello swordams and cydonia,

I had to ask both a skilled programmer and my super logic savvy girlfriend to help me explain this one.

In short, the condition to mark when to end our loop will never be met that way.

Explained:
Let's change the second part of our loop to be j < j + 4. The idea of the second part of a loop is to mark when the computer stops running the loop. We run our loop until j isn't less than itself + 4. When the loop starts again, it looks at the third part (j++) and then increases j by 1 and then runs the test of if j is less than j + 4. Problem is, j will ALWAYS be less than itself plus 4 because of our third part of the loop. It wont end, thus making it an infinite loop. That is why the correct answer is to use i, not j.

Hope this helps!


#4

chipsurfer94275,

Thank you, your logic is very clear. However, I still don't understand the way the code is working.

My original guess was to write

for(var j = i; j < (myName.length); j++) {
hits.push(text[j]);

which I thought would mean it would push each character to `hits` until it had the number of characters equal to the length of my name. I'm not sure why it's `myName.length + i` and not just `myName.length`.


#5

@cydonia @chipsurfer94275 @swordams

The for loop which swordams initially specified runs INSIDE the other one, this will work the way you have presented, and doesn't need to be changed. Yes, the i value will increase, but when it does then the j loop will've finished running and be waiting for the next i value to test. The loops don't run at the same time, so there won't be an infinite loop, and it will run correctly. Please ask a question if further explanation is needed.


#6

I apologize for the life story in advance, I hope this helps.

The code makes more sense if you have several words that start with the same first letter as your name. For instance, my name is David, if I have names like Darryl and Dillon in my text, the hits array will include D-a-r-r-y(no L) and D-i-l-l-o(no N) because those characters in the (parenthesis) exceed my names length of 5 characters. The program isn't specifically looking for your name, it is first looking for the first letter of your name that you reference in (text[i] === 'D') and then focusing on the length of myName. So if I had Do and Dare to... in my text variable, Do an + Dare (That is DoSPACEan and DareSpace) would also be included because they have a capital D and a total of 5 characters.

So the code is...

if (text[i] === 'D') - Find the character D (case sensitive will not register "d")

and

for (var j = i; j < i + myName.length; j++) - Find all instances of the letter D that matches myName.length so in this case any instance of the letter D and the four following characters (you are not adding the value of i and myName.length)

and

hits.push(text[j]); - Add all grouped characters that start with D and have 4 characters after it to the hits array

and

console.log(hits) - Display all grouped characters in the hits array, in this case
[ 'D', 'a', 'v', 'i', 'd', 'D', 'a', 'r', 'r', 'y', 'D', 'i', 'l', 'l', 'o' ]

/*jshint multistr:true */
var text = "My name is David and this \
is my attempt at going through this lesson\
that wasn't very clear my first time through it\
it may however be more clear to people named \
Darryl or Dillon";
var myName = "David";
var hits = [];

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

if (hits.length === 0){
console.log("Your name wasn't found!")
} else {
console.log(hits);
}