7/7 I don't understand!


#1

I don't understand the logic behind this!

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


#2

Please be more specific, your question is too broad, it's a time sink and some may avoid answering at all as a result. There also is not a question, we'd have to formulate that ourselves.

Explain what you think should happen.
Explain in what way it appears to behave differently from that.
Add a clear, specific question.

If you want somebody to describe what is happening line by line, say so do be aware though that your chances of a fast reply are lower than if you explain what the confusion is about, and you may get a less enthusiastic reply as well.


#3

Ill break it down for you:

  1. We have a variable text set to a string of text. The goal is to find your name in that text, push it to an array, then console the array to see your name.

In the var text, forget you are looking at letters. In the first for loop its counting every letter in the string as a number. So if in my string I have var text = "Hello Chris and World"; My first for loop is counting 18 letters. 1, 2, 3, 4, in each loop until i = 0 is bigger then 18 so it stops.

Using my name Chris as an example. We count all the letters and go through one by one. Our if statement is saying if one of the spots we are counting = "C" - then run the next loop. In my above string, C falls on the 6th spot.

Thats where we get to the logic behind the second for loop. We found C. So next we want to push my entire name to the array. How do we find h, r, i, s? We don't. Instead, we count the length of myName which is 5 letters for chris. So we found C, and we count and know that my name has 5 letters. Our first loop also found C on the 6th character spot in our string. So all we have to do, is push 5 characters starting at the 6th spot to our array which will be my name.

So if we push 6 (C), 7 (h), 8 (r), 9 (i), 10 (s) spots to our array, we need to tell it to stop after it pushes 5 letters. So let's look at your code below.

text[i] is saying going through each of the numbers of string text in our first loop, if we find s, execute code block below
if (text[i] === "s") {

In my name C is found at the 6th count. So we want to start at 6 because that is where my name starts. That's var j = i So we know Chris is 5 characters so we need to get 5 characters and stop. But we want it to stop after my name ends. Starting at the 6th spot, add 5 which is the length of my name = 11. So we want to loop 6 - 10 and stop on 11.
for(var j = i; j < (myName.length+i); j++) {

Here, each time our second loop runs, 6, 7, 8 , 9, 10, stop, it's pushing the 6th spot, the 7th spot of my string to the array. So it's pushing C, h, r, i, s. it stops when j is 11 so it doesn't push any more text characters from my string
hits.push(text[j]);
}
}
`


#4

Forgot to mention, so once it stops after it finds Chris or reaches 11, the Outer for loop will continue going through the count of string text until it finds C again, if I listed my name multiple times, and then will repeat the process all over.