Search Text for Your Name for loop.. lost


#1

Search Text for Your Name challenge;

Can you help me work out the thought process behind this problem?

I have been following along smoothly until this problem, which is just totally out of my league. I have a decent understanding up to this point, but now I have no idea whats going on and the explanation was lacking, and the jump too far.

I would really appreciate if someone could help explain whats going on and how to go about this!

var myName = "Mark";
var hits = [];

for (i = 0; i < text.length; i++) {
if (text[i] === 'M')
{
}

I am following along up to this p oint; but the second for loop is where I am lost.

It says

your second should be;

for(var j = i; // rest of loop setup

without explaining why, I am not sure why we have to change to this new variable, but just acepting that I am still lost.

Finally, in the body of your loop, have your program use the .push() method of hits. Just like strings and arrays have a .length method, arrays have a .push() method that adds the thing between parentheses to the end of the array. For example,

What is the push() method of hits? I imagine we want to push a "hit" (correct letter) to the array hits[].

for(var j = i; j < (myName.length); j++) /* this part seems incorrect to me.. but I don't know why */
{
hits.push([j]); /* J */
}

if we are increasing the new j variable until it hits the length of our name, what do we have to do anything with i for? How do these two variables relate besides j=i ?

Can anyone help work this out?


#2

you know that strings are indexed, if you have the text: Hello Mark. H = 0, e = 1, l = 2, l = 3, o = 4, space = 5, now M = 6. Now, the first for loop:

for (i = 0; i < text.length; i++) {

will give over your text, the moment i = 6 we have a hit , the if condition: if (text[i] === 'M') gets executed. Does this make sense?

now you want push your name into hits (since you want to print your name in the end), how do we do this? anohter loop:

for(var j = i; j < (myName.length); j++)

now, we assign i to j. What is i? 6, remember? The start of Mark. normally we assign a number when we start a loop, but we can assign a variable to it. now, j knows where you name starts, then myName.length will go over your name, and push the hits in your var hits = []; variable. (with hits.push([j]);`).

Does this make sense?


#3

thank you for the clear reply!

you know that strings are indexed, if you have the text: Hello Mark. H = 0, e = 1, l = 2, l = 3, o = 4, space = 5, now M = 6. Now, the first for loop:

for (i = 0; i < text.length; i++) {
will give over your text, the moment i = 6 we have a hit , the if condition: if (text[i] === 'M') gets executed. Does this make sense?

this is very clear to me.

for(var j = i; j < (myName.length); j++)

this part is more confusing, new variable J, if it is less than (4), increase until 4.
Then hits.push([0-4]); using "J" as a substitute , right? hits.push([j]);

but this is not working (so far I have this)

/*jshint multistr:true */

var text = "My name is Mark and I am a guy and I have a fish named Ruby and I am mark Mark Mark.";

var myName = "Mark";
var hits = [];

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

console.log(hits);

this logs "[[0], [1], [ 2], [3],]" which seems to be correct, at least in the number of digits it logs and in the order, but its not printing my letters " M A R K " .

The error says "Careful your second 'for' loop should stop when it reaches current point in the string + myName.length
--this error is confusing; why would it need to go beyond myName.length? My name is 4 characters long, why would it need to go to 5 characters?


#4

you do understand that:: `var j = i; you assign the value of i (6 from my previous example) get's assigned to j. if you do not understand, do:

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

this will show you what the value of j is. but myName.length would return 4 (counting starts at one), but your string starts counting at zero (zero indexed) (so, if counting starts at zero, the last character is indexed at 3, not 4 (remember that length return 4)), so that is why you need length - 1