Push arrays


#1

<PLEASE USE THIS TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/courses/javascript-beginner-en-XEDZA/0/5?curriculum_id=506324b3a7dffd00020bf661#

<Below this line, in what way does your code behave incorrectly? Include ALL error messages.>
While trying to push characters of myName to Hits. I have tried myName.push.length and such but can’t seem to make anything work.



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


#2

please post your full code here then someone can try to help

right now i can just tell

if (text[i] === myName)

here text[i] contains just one word but myName doesn’t so it’ll never be true.

and what did you expect from

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

why did you set j=0 ?what’ll you get by myName[i].length ?


#3

I would like to push myName to the Hits array.

so it should be something like myName.push(); or hits.push(myName);

is even close??

any way this is the extent of the code…

var text = “Yogy bear had no hair”;

var myName = “Hitch”;

var hits = [];

for (i = 0; i < text.length; i++){
if (text[i] === myName){
for (var j = 0; j < myName[i].length; j++){

    }           
    
 }  

}


#4

what i already told you

if (text[i] === myName)

what is text[i] ?? and what is myName? will it be true or false?

Lets go through it step by step
At first step

if( text[0] ==="Hitch") // i=0 and myName=“Hitch”

step 2

if( text[1] ==="Hitch")

so on…

you have to check whether or not the word(text[i]) match with the first word of your name.


#5

ok so the the index of text is either H or Hitch.
so to set the index of text to [0] it would take the first character or name.
So I made this === true because it states the array.

to code the second for loop I am having much difficulty.

var text = “Hitch bear had no hair”;

var myName = “Hitch”;

var hits = [];

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


#6

if (text[0] === true)

you shouldn’t check the condition like that.what is text[0] ? it’s the first letter of your text variable. right?right now your first letter of text variable is H But what if it’s not H? may be it’s(H) at index no 3 or 5? if you set text[0] it remains same all the time what we don’t want.That’s why we are using loop here.
and why are you checking whether or not it’s true? we want to find myname from the text variable if there is. so
set

if (text[i] === 'H')

why did i set like that?

suppose the text variable is

text='i am Hitch'

then it’ll still work. It doesn’t matter where the letter H is in the text variable.
and here

var j = 0

here you have to set j=i.because we want to start the loop from where it finds the letter(H).

now inside the second for loop add .push() method.

have a look at the example in the lesson for push() method.


#7

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