I have the right code but dont understand it!?


/*jshint multistr:true */
var text = "fwebinawjedeinPatrickfvzgebuhni \
zvtguzbhiPatrickfvzgbuhnPatrickcfvzgbuhnfie \
var myName = "Patrick";
var hits = [];

for (var i = 0; i < text.length; i++) {
if( text[i] === "P") {
for (var j=i; j < (i+myName.length); j++)
if(hits.length === 0) {
console.log("Your name wasn't found!");
} else {

the explanation from codecademy is this:

1.Loop through the array,
2.Compare each letter to the first letter of your name, and if it sees that letter:
3.It will push that letter and all the letters that follow it to an array, stopping when the number of letters it pushes are equal to the number of letters in your name.

My problems:

  1. var text is a variable and not an array right? so it should say the code loops throught the text variable not though "the array"...
  2. how does the loob "know" what variable to loop through? why not loop through myName? text.lenght just tells the for loop how long its supposed to look for a P not where, right?


You accidentally put the bracket in the wrong place there. :smiley:


@microsurfer68683 for your first question, yeah if that's how they worded it, it could have been phrased better. For your second question, this line tells the program what to loop through:

if( text[i] === "P") {

It will look through the data in the text variable, character by character, using i as the counter/marker.