Your "second" for loop


#1

Hello, I'm not even sure I understand the directions, and really not sure how to code the push part. Could someone please take a peek and tell how bad this is?? thanks

var text = "Hey my name is Diane.  How are you doing today?.\ My full name is Diane Roy and  my mother said she \ named me Diane because it was her favorite name! \ Diane loves dogs! Diane needs a glass of wine!";

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

#2

Can you tell me what kind of error do you get? But I see incorrect syntax in some places.


#3

These two errors : Oops, try again. Careful: your second 'for' loop should stop when it reaches its current point in the string + myName.length.

and myName is 'undefined'


#4

Hi Diane,

A couple of things.
The text is not a variable so doesnt need var infront of it but this doesnt really matter and wont give an error, just FYI.
The idea behind placing a backslash \ is so you can then put a line break and javascript will know that its not a different line of code and you've done that to make it easier to read, like so.

text = "Hey my name is Diane. How are you doing today?\
My full name is Diane Roy and my mother said she \
named me Diane because it was her favorite name! \
Diane loves dogs! Diane needs a glass of wine!";

//This way you don't have one long line.

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

// the above lines are fine

for (var i = 0; i < text.length; i++){
// its i < text.length rather than i <=text.length because the text file begins at 0 not 1 so if the file is 5 letters long the last character is located at position 4, as its [0, 1, 2, 3, 4]

if (text[i] === 'D'){

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

// again j < myName.length rather than j<= .....
// as you want to capture your whole name you want to run the for loop from the first letter at position i until the last letter which is at position i + (the length of your name).
// remember that j+= means j + (nothing ) = j. Using the j+= expression you could write j+=3 that would increment every 3. In our case we only want it to increment 1 so you could write j+= 1 or by convention its just j++

    hits.push( text[ j ] );

// you need to tell the computer that you want it to place the letter positioned at j (which you know is at position D) in the text file into the array called hits. The for loop will do this for every letter in your name.
}
}
}

// Long winded answer but hope this explains where you have gone wrong


#5

I don't get this. Wouldn't putting < text.length mean that the last position fo text wouldn't be covered, whereas <= would ensure that every single position until the last one would be covered?


#6

Think about the text var is "I'm your father".
If you use console.log(text.length), you'll find out the number 15, right? (count spaces too)
If you want to run all the array (text), you should run all the 15 positions, right?
Arrays starts with the position (index) ZERO, then a FOR to run (visit) the whole array should be something like this:
// "for (var i = 0; i"<"text.length; i++){code code code}"
Why "<" and not "<="?
If you use <= the FOR LOOP will try to visit 16 positions in an array with only 15. That's because arrays start with the index 0 (zero).
But if you use "<", the FOOR LOOP will visit 15 positions because it will stop at position less than 16 (in this exemple).

Understand? Sorry for my long explanation :wink: