简体   繁体   中英

Javascript stuck at “for” loop

i am newbie learner and i am learning basic javaScript from codecademy .I stuck at "Search Text for Your Name" tutorial 5/7.

here is my question:

your loop should stop when it hits the value of the first iterator (say, i) plus the length of your myName variable.

here is some informations from to tutorial:

Your second "for" loop Okay! Last loopy step: add another for loop, this time inside the body of your if statement (between the if's {}s).

This loop will make sure each character of your name gets pushed to the final array. The if statement says: "If we find the first letter of the name, start the second for loop!" This loop says: "I'm going to add characters to the array until I hit the length of the user's name." So if your name is 11 letters long, your loop should add 11 characters to hits if it ever sees the first letter of myName in text.

For your second for loop, keep the following in mind:

First, you'll want to set your second loop's iterator to start at the first one, so it picks up where that one left off. If your first loop starts with

> for(var i = 0; // rest of loop setup 

your second should be something like

> for(var j = i; // rest of loop setup Second

think hard about when your loop should stop.

Finally, in the body of your loop, have your program use the .push() method of hits. Just like strings and arrays have a .length method, arrays have a .push() method that adds the thing between parentheses to the end of the array. For example,

newArray = [];
newArray.push('hello');
newArray[0];   // equals 'hello'

and here is my code:

multistr:true

var text = "Hey, how are you \
doing? My name is Emily.";
var myName = "Emily";
var hits = [];

for (var i = 0; i > text.length; i++) 
{
   if (text[i] === 'E') 
   {

    for(var j = i; j > text.length; j++){


    };



    }; 
};

ps: i don't want to pass this tutorial without understand it. please help me. teach me.

for (var i = 0; i > text.length; i++) should be
for (var i = 0; i < text.length; i++)

otherwise it won't ever meet the criteria to even start the loop.

Welcome on board! You confused > with <. Your loops won't run because for the first check when i = 0 it certainly does not hold that 0 > text.length, because text.length is at least 0 (there are no strings shorter than the empty string).

You should make a habit of manually going through your loops for the first two steps and then check what happens just before the loop ends.

Here is what I got for my code:

for ( i = 0; i < text.length; i++)
{

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

}

};

It looks like you were missing the " + i " part in your second for loop. That seems to make sure that the first loop will be included. I tried it without the "+ i" and it does not work.

I tried continuing directly from the second for loop using a "+ j" and that only crashes the browser.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM