Search Text for Your Name: Your second "for" loop


#1


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


I am receiving a message: "Oops, Try again. Careful: Your second 'for' loop should stop when it reaches its current point in the string + myName.length. " I read the comments and tried several things but nothing seems to be working.


var text = "Melinda Melinda Melinda";
var myName = "Melinda";
var hits =[];

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


#2

So mine is set up almost exactly like yours. A difference I have though is when I start my second for loop, I don't define j as a variable, I just say for(j = i;)
ADDITIONALLY, in that same for loop, you need to make sure that you are saying that j < i + myName.lenght;
Don't forget to add. :smile:
Lastly, inside of your second for loop, instead of all that newArray stuff, you only need one line which says hits.push(text[j])

I hope this helps! Let me know! :smiley:


#3

The directions say to do what I've done in the code. I just discovered the i + name.length issue after looking at it for over an hour! LOL....That solved my original problem.

Now, however, I have another issue. "It looks like your 'for' loop isn't pushing values to the hits array. Make sure that it's working properly and that myName's text appears somewhere in the text variable."

The directions say to do this: "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, (and then it shows the newArray lines of code.)


#4

@maclare,

for (var j = i; j < myName.length; j++) {
   newArray =[];
   newArray.push('Hello');
   newArray[0];
}

newArray created inside for loop. So outside for loop it is not exist. You need to use var hits = []; (defined in line 3) instead newArray:

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

and push there letters of your name.

The directions say to do this: "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, (and then it shows the newArray lines of code.)

It's just instruction that says to use Array's method push() and shows example how to use it:

newArray = []; // defined Array Object
newArray.push('hello'); // used push() method to put there string value
newArray[0];   // now newArray has 1 element "hello"

Also try to add to var text = "Melinda Melinda Melinda"; other strings. For example:

var text = "bla bla Melinda Melinda text Melinda text";

And try to understand why we used j < i + myName.length;

for (var i = 0; i < text.length; i++) {
    if (text[i] === "M") { // here we want to find the index where is 'M' (i = 8)
        for(var j = i; j < i +  myName.length; j++) { 

// we need nested loop to push letters from only myName text (not other symbols), 
// and we start to do this from index where text[i] === "M" (it's 8). That's why you 
// need add i to myName.length 
// var j = i -> start index
// j < i + myName.length -> push myName's symbols from start index i

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

Second variant do the same work but, as for me, is more easier for understanding.


5/5 please help
#5

Hi
So im very confused, here is what i tried

var text = "blah blah blah blah blah blah blah blah \
blah blah blah blah blah blah blah blah blah blah blah \
blah blah blah blah blah Georgia blah blah blah blah blah \
blah blah blah blah blah blah blah blah blah blah blah \
blah Georgia blah blah blah blah blah blah blah blah blah \
blah blah blah blah blah blah blah blah blah blah blah \
blah blah blah blah blah blah blah blah blah Georgia blah \
blah blah blah blah blah blah blah blah blah blah blah"
var myName = "Georgia"
var hits = []
for ( i = 0 ; i < text.length ; i++) {
    if (text[i] === "G") {
        for ( j = i ; j < (i + myName.length) ;  hits.push("Georgia");
    }
};

because i'm not fully grasping the .push concept. After reading this thread I tried

var text = "blah blah blah blah blah blah blah blah \
blah blah blah blah blah blah blah blah blah blah blah \
blah blah blah blah blah Georgia blah blah blah blah blah \
blah blah blah blah blah blah blah blah blah blah blah \
blah Georgia blah blah blah blah blah blah blah blah blah \
blah blah blah blah blah blah blah blah blah blah blah \
blah blah blah blah blah blah blah blah blah Georgia blah \
blah blah blah blah blah blah blah blah blah blah blah"
var myName = "Georgia"
var hits = []
for ( i = 0 ; i < text.length ; i++) {
    if (text[i] === "G") {
        for ( j = i ; j < (i + myName.length) ;  hits.push(text[j]);
    }
};

I dont understand why we use (text[j]) though?

but then I still get Syntax error unexpected token so not sure where to go from here


#6

The explanation in this link will help you:


#7

I dont understand why we use (text[j]) though?

@georgiamayou, variable text has your name. So, in this variable you need to find where is your name and push from text to result array hits your name's symbols.

if (text[i] === "G") // it means that you want in variable text find the index where is symbol 'G'

If 'true' you need to know how many symbols push into result array hits.

if (text[i] === "G") { // you can to find index where 'G' is. If condition is true then we can use second for loop
        for ( j = i ; j < (i + myName.length) ;  j++ ) { // here you have wrong for loop syntax (previous post can help you)
            hits.push(text[j]); // pushing from variable text (because text has your name) to array hits
    }

#8

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