Walk me through this? I understand everything until we add in "var j"


Can you please walk me through this? I understand everything until we add in "var j".

5/7 Careful: your second 'for' loop should stop when it reaches its current point in the string + myName.length

var j is just a random variable we're using, as it's not likely you'll use j for anything else.

You use the second for loop to push the results into the array hits.


Can you please explain to me the significance of "text[i]", setting var j equal to i, and why j must be less than (myName.length + i)?

I promise I'm not an idiot, I'm just really confused about this specific aspect of JavaScript. :joy:


text = "Given a fairly large block of text, Rocky, in which \
    the user's name is, 'Rocky', the value for `j` each time \
    a match for 'R' is found will be whatever is the current \
    value of `i`, the outer loop iterator variable.";
myName = "Rocky";

The above assumes jshint is included at the top of the code.

The first match above is at index [36] (so, i == 36). i is the offset from the start of text, and we wish to iterate over five characters in total, including the R.

      ... 36  37  38  39  40  41
text[j] == R   o   c   k   y
         j = i;          j < i + myName.length;  j++


I apologize, but I'm still struggling to understand. Is there a video explanation that somebody could link me to?


What do you not get. The for loop? String indexing? The opening example in lesson 1?

Might help to go back and review the topics 'strings' and 'arrays'.

Given a string,

text = "This is a Rocky string of some length."
myName = "Rocky"

for (var i = 0; i < text.length; i++) {
    if (text[i] === myName[0]) {
        console.log(i, text[i]);

Remember, the first index in a string is 0, so the first letter is at index [0]. That would be the first letter in "Rocky".

The output from above will read,

10 R

myName.length is 5, so setting j to 10 and the upper bound to 10 + 5 will allow the inner loop to iterate over only those five letters in text, pushing each one to the hits array.

     i === 10
          j = i;          j < i + myName.length;  j++
     j === 10  11  12  13  14  15
text[j] === R   o   c   k   y

The inner loop stops when j++ is 15.


After browsing the internet and my local library, I discovered just how much I have yet to learn.

I'm a beginner programmer. I know basic HTML & CSS (paragraphs, headers, tables, etc), but JavaScript was the first real programming language I was attempting to learn. I have no background in programming, I've only been doing this for a month or so, and I am so uninformed regarding syntax and programmer lingo.

My reading led me to conclude that JavaScript is not a good beginner language to learn, so I started learning Ruby a couple days ago. I'm going to come back to JavaScript after I familiarize myself more with programming.


Whatever language you attempt will not make a lot of difference. It's tough going at the start with so much to learn. I think if you already have some HTML and CSS under your belt you should stick with JavaScript so you can tie the knot on browser basics. All three are resident in any browser with no other software needed but a text editor.

JavaScript is the language that is most common in web development and something you can put to use right off the hop. Study the fundamentals, practice and read. In a couple of months you will have a much better handle on it.


I will learn JavaScript soon, but Ruby is making a lot more sense to me at the moment. I'm not giving up on JS, I'm just trying to ease the learning curve.

Also, I'm only a senior in high school and I have no immediate uses for programming (such as web development) at the moment. I'm going to Purdue University to major in computer science, and right now I'm just trying to get a head start on programming. Plus, I am having fun!


Ruby is a fun language, and very powerful, to boot. It is a good language to have under one's belt, even if we stop at the fundamentals. The web side of Ruby is Rails, but for that you will need to brush up on HTML and CSS since it is document related. Eventually you will find that there are just some things that JavaScript does best, so be sure to schedule this in sometime before summer.

No doubt, a CS course will start you off with C++ or Java. You'll find both of these languages more closely similar to JavaScript than Ruby, at least the general constructs. No, Java and JavaScript are not related in any way except by name. There are a great many similarities, though.

Happy coding!