How do I use push?


#1


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


"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"
my name is in there. I just don't know how to use the push command properly


/*jshint multistr:true */
var text = "super people Ben please don't find the\
right way to Ben say that not about me, but about Ben\
my country needs Ben right now. And they're not gonna get\
him, because he's Ben right now. He's too good for them."
var myName = "Ben"
var hits = []
for (i=0;i<=text.length;i++) {
    if (text[i]==="B") {
        for (j=i; j >= i+myName.length; hits.push("Ben"));
    }
    else {
    }
}


#2

Here's my code:

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

push() appends the text to the end of your list, you don't want to put hits.push as the third argument in the for loop, it needs to be j++ or j-- or something similar


#3

Hi @georgetrent,

If it may help please see the code example below.
So there are a couple of areas to cover here :), following along with the code comments, we'll take advantage of the value in the var myName, which is what we are looking for, and try to match it up by index on the first letter. myName[0].
Then we just need to address the second for loop, here the increment j++ is missing and in its place you have used the hits.push() method, the second is that you have used a >= rather than a <=, and then we can tell the for loop what action we want it to perform inside its script block; and in this case we want it to push the letter, by index, it found into the hits array. In this case it will 'push' as many letters in as the length of the var myName :slight_smile:

The Array push() method itself appends whatever you pass it as an argument/parameter onto the end whatever object you are working with: So it pushes in, at the end of the array hits[]. In this way we get the output for our name, to whatever we put in the var myName.
Hopefully that's not too confusing, if there is anything unclear please feel free to reply and someone will try to help as soon as possible :slight_smile:

/*jshint multistr:true */
var text = "super people Ben please don't find the\
right way to Ben say that not about me, but about Ben\
my country needs Ben right now. And they're not gonna get\
him, because he's Ben right now. He's too good for them."
var myName = "Ben"
var hits = []
for (i=0;i<=text.length;i++) {
    if (text[i]=== myName[0]) { //let use the var myName and try to match up the letter we need by index
        for (j=i; j <= i+myName.length; j++) //change from > to < and add the j++ increment, remove the semi-colon
        {
            hits.push(text[j]);
        }
    }
    else {
    }
}

#4

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