Help PLZ 2nd loop


#1

/*jshint multistr:true */
var text = "Hey, how are you \
doing? My name is Steven.";
var myName = "Steven";
var hits = [];
for(var i = 0; i < text.length; i++);
if (text[i] === 'S')
{
for (var j = i; j < myname.length; i++)

}
what's Wrong?!!! (easy instructions i'm only 10 years old!!)


#2

SRY wrong one
var text = "Hey, how are you \
doing? My name is Steven.";
var myName = "Steven";
var hits = [];
for(var i = 0; i < (myName.length + i); i++);
if (text[i] === 'S')
{
for (var j = i; j < myname.length; i++)

}


#3

Alright, so the lesson is telling you it wants to put your name into the "hits" array. In order to do that you need to find where in the variable "text" your name starts, and then put each letter into the array.

I understand where your logic was taking you but unfortunately you're a little backwards

for(var i = 0; i < (myName.length + i); i++); /*the condition should be in the other for
loop. Also, it's for the length of the text, and not the length of your name.*/

if (text[i] === 'S') //This is correct
{
for (var j = i; j < myname.length; i++) //switch these conditions, and change i++ to j++

}

You'll want to increment only the variable in the for loop that you're creating, so when you're initializing j, don't increment i.


for(var i = 0; i < text.length ; i++);
if (text[i] === 'S') 
{
for (var j = i; j < (myName.length + i); j++)
/*                     ^ the variable is case sensitive. it can be "myName" but not
                         myname, or MYNAME. Case sensitive means its stubborn!        */
}

This is complicated so let me explain what it's doing.

the first for loop is going through every letter in the variable "text" which is the string you have here: "Hey, how are you \doing? My name is Steven.";

As soon as it sees the letter "S" as marked by your if statement: "if (text[i] === 'S')" it goes through your second "for" loop.

Your second for loop will take the length of your name "Steven" which is 6 letters, and will print out the next six letters that it sees. Assuming you don't have a bunch of "s" in your variable your code will print out "Steven" just as it was told. But you'll learn where it prints to in the next lesson.

For further use, it prints each individual letter to the variable array "hits" , giving the array separate character values looking like this:

["S", "t", "e", "v", "e", "n"].

I hope that answers any questions you have.


#4

I still don't understand what is wrong with these lines of code. The console says "SyntaxError: Unexpected token }"

/*jshint multistr:true */
var text = "Hey, how are you \
doing? My name is Steven.";
var myName = "Steven";
var hits = [];
for(var i = 0; i < text.length; i++);

if (text[i] === 'S') {
for (var j = i; j < myname.length; j++)
}


#6

(post withdrawn by author, will be automatically deleted in 24 hours unless flagged)


#7

I finally got it thank you so much