How do i get the set up of the push method correct?


#1

this is what i have so far

var text = "hey how are you \
doing? my name is William.";
var myName = "William";

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

}
}
}

#2

Syntax:

Array.push(object);

as in,

hits.push(text[j]);

#3

do you mean like this?

var text = "hey how are you \
doing? my name is William.";
var myName = "William";

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


#4

Remember to apply the .push() method. See my earlier example.

Two errors in the above line. Should be,

for (var j = i; j < i + myName.length; j++) {
                    ^                  ^
                    1                  2
  1. Must use an offset so we capture from the right position in text.
  2. Must use the control variable of the loop when incrementing.

#5

thanks so much i got it :smile:


#6

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