I can't understand the meaning of line


#1


https://www.codecademy.com/courses/javascript-beginner-en-XEDZA/0/7?curriculum_id=506324b3a7dffd00020bf661


I just don't really understand what this line means, and I read theory and many times, but cant't comprehend how computer understands this code: for (var j = i; j < (i + myName.length); j++) {
hits.push(myName); }


Replace this line with your code. 
/*jshint multistr:true */
var text = "Hey, I'm Anna, fjlsghfbghr, Anna, jfjgbsldfbadgh;gb, Anna, dhsjfbsfhgbfjdf,jfgbrgg;, Anna";
var myName = "Anna";
var hits = [];
for (var i = 0; i < text.length; i++) {
    if (text[i] === "A") {
        for (var j = i; j < (i + myName.length); j++) {
            hits.push(myName);   
            }
        }
}

    if (hits.length === 0) {
        console.log("Your name wasn't found!");
        } else {
            console.log(hits);
        }


#2

Let me explain the program in simple words.

1.There is a text in which your name exists(or maybe not)
2.You stored your name in a variable.
3.You created an empty list
4.Your first for loop go through each character of text
5.There is conditional statement if it checks if the first letter of your name and the character of text are equal or or not. If it is equal then..
6.Inside loop then goes over same amount as length of your name
7.inside the body of your second for loop,you had to push characters not myName
(You need to make a change there) into the empty list you've created at step 3.


#3

It outputs such data: [Anna, Anna(and so 16 times)]. Could you please explain why? I mean why not just 4 times, like in text?


#4

Reason:
back to previous section (read theory and instruction of this exercise)
https://www.codecademy.com/en/courses/javascript-beginner-en-XEDZA/0/6

And I already mentioned in step 7.


#5

Yes, you already mentioned it in step 7. But it's obvious I don't know what I have to do, how can I replace it, so I asked for a little bit more detailed answer, but whatever(Instruction may explain something but not everything)


#6

Instructions and theory are like 80% of each exercise. and other 20% is just what a person have to go with logic. (Without revising previous exercise no one can understand later part)

Now back to your question..

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

lets take an example..
myName = "Ryan"

and if loop starts at 4 (and yeah matched at 4- text[i] === "A").. then you don't write this,,

for( j = 0; j < myName.length;j++)
but you write this..

for( j = 4; j < 4 + myName.length;j++)

Problem2.
hits.push(myName);
As I said You don't have to push name but characters where the match starts?
so where the match starts ?
when text[i] === "A" turns out to be true, so you've to push text[i] ?
In that step you have to push the characters of text at which it matches with first letter of your name.

If its find your name then It should have pushed your name characters so your hits Array would look like this...

["A", "n","n","a"]


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.