How am I using the .push() statement incorrectly?


#1


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


Hey coders! I'm having a problem with my push statement. Every single time I run my program, codecademy always comes back at me with
"Oops, try again. It looks like your second 'for' loop isn't pushing values to the hits array. Make sure it's working properly and that myName's text appears somewhere in the text variable."
See that seems pretty self explanatory, but I don't really understand it cause I'm dumb XD.
I think I most probably have a syntax error, but I'm having trouble identifying it. Help would be appreciated. Thanks!


Replace this line with your code. 
/*jshint multistr:true */

var text = " Hi! Srinjoy's name is Srinjoy! Srinjoy \
likes to talk in third person!"
var myName = "Srinjoy"

var hits = []; 

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


#2

you don't have a syntax error, but you do have a problem in this line:

if (text[0] === "S" )

you want to use text[i] here, to check if the current letter is the first letter of your name, then you have a problem here:

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

the second for loop should loop over your name, now that i contain the index of the first letter of your name, you can loop over it by setting j < i + myName.length


#3

Cool! But if I did what you recommend would j still equal I in the second
for loop?


#4

you use j to loop over your name, which results in:

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

this way, j will loop from the first character of your name till the last, i wrote detailed answers about this, you can read them:


And then ask if you got any questions


#5

Thanks! That helped a lot. I just don't get the second for loop. I know it's doing something along the lines of scanning the variable "text" for the rest of the characters in my name, I just don't know exactly.


#6

those answers are time consuming to write, read ones (or find other ones) first:



#7

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