5. Second for loop


#1

Help me. What is wrong in my code?

var text = "hghfjhf jfdh djdfj ijhdj jgdjdj jdjk dgdg dfhd djhfdjh difhdhf fdf Pamela dsjb fjdkj Pamela dgshjfjhfhj hf jdhk jd Pamela";
var myName = "Pamela";
var hits = [];
for (var i=0; i<text.length; i++) {
    if (text[i]==="P") {
       for(var j=i; j<(myName.length + i); j++) {
           hits.push(myName[j]);
        }
    }
}

#2

your variable myName already contains your name, why push it into the hits array? I would get your name from the text variable. The purpose of your program is to get your name from the the text variable, so that is what i would push into your hits array


#3

app returns this answer:
Oops, try again. Make sure you're pushing letters to the hits array!


#4

Did you see my answer?


#5

i don't understand what you try to say me


#6

this line:

hits.push(myName[j]);

you try to push letters from your myName variable into your hits array, why? You want to get your name from the string stored in your text variable. That is the purpose of the program, not to get your name from the myName variable. (let aside the index error you get with trying to get your name from the myName variable)


#7

ooo thanks a lot :wink:
you are right