简体   繁体   English

试图了解javascript中的for循环

[英]trying to understand the for loop in javascript

Hi I am currently doing a javascript course at Code Academy pertaining to "for" loops. 嗨,我目前正在Code Academy学习有关“ for”循环的javascript课程。

but this exercise does not make sense to me and it is pointless going further without understanding why I am doing what i am doing... here with the code. 但是此练习对我来说没有意义,并且如果不了解我为什么要执行我的工作,那么继续前进是毫无意义的。

text = "Blah blah blah blah blah blah Eric \
blah blah blah Eric blah blah Eric blah blah \
blah blah blah blah blah Eric";

var myName = "Eric";
var hits = [];

// Look for "E" in the text
for(var i = 0; i < text.length; i++) {
  if (text[i] === "E") {
      // If we find it, add characters up to
      // the length of my name to the array
    for(var j = i; j < (myName.length + i); j++) {
        hits.push(text[j]);
    }
  }
}

if (hits.length === 0) {
  console.log("Your name wasn't found!");
} else {
  console.log(hits);
}

I get the first loop but what confuses me is the whole second loop inside the if statement.... 我得到了第一个循环,但令我困惑的是if语句中的整个第二个循环。

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

just dont understand what i am doing? 只是不明白我在做什么?

The second loop is looping is basically adding the rest of the letters for your name into the array 第二个循环是基本上将名称的其余字母添加到数组中的循环

j = i --> This means that it will start at the position of i, which is where it found an "E" in your case j = i->这意味着它将从i的位置开始,即在您的情况下找到“ E”的位置

j < (myname.length + i) --> is taking the length of your name + i which will give you the position up until the end of your name j <(myname.length + i)->占用您的名字+ i的长度,这将使您的位置一直到名字结尾

j++ --> this is incrememting j by 1 j ++->这会使j增加1

hits.push(text[j]) --> This will push the next letter to the hits array based on the j index hits.push(text [j])->这将根据j索引将下一个字母推送到hits数组

So basically, the inner loop starts at the first letter of your name, then runs the length of your name, adding the rest of the letters from the string to the hits array, which ends up adding your the letters of your name into the array 因此,基本上,内部循环从名称的第一个字母开始,然后是名称的长度,然后将字符串中的其余字母添加到hits数组中,最后将您的名称的字母添加到数组中

You will end up with the array looking as such hits = {E,r,i,c,E,r,i,c,E,r,i,c} 您将最终得到看起来像这样的命中的数组= {E,r,i,c,E,r,i,c,E,r,i,c}

It starts a new loop from the index where the character E was found in the initial text up until the myName.length + i index, which bascially loop over the substring described by myName . 它从在初始文本中找到字符E的索引开始,直到myName.length + i索引,开始新的循环,该循环基本上遍历myName描述的子字符串。 For each iteration of that loop, it's adding the currently iterated over character to hits . 对于该循环的每次迭代,都会将当前迭代的结束字符添加到hits

Please note that the code infers that everything starting with E is equal to yourName , which is quite naive. 请注意,代码推断所有以E开头的内容都等于yourName ,这yourName幼稚了。

Why myName.length + i ? 为什么是myName.length + i

  1. Imagine you have this text: aaaaEric . 假设您有以下文本: aaaaEric
  2. Then you loop until E , so i = 4 . 然后循环直到E ,所以i = 4
  3. Now you want to loop over Eric in the subloop. 现在,您想在子循环中循环遍历Eric
  4. If you do not add i to the condition, you end up with j = 4; j < 4; j++ 如果不将i加到条件中,则最终得到j = 4; j < 4; j++ j = 4; j < 4; j++ j = 4; j < 4; j++ , which will not loop at all. j = 4; j < 4; j++ ,它根本不会循环。 You must add where you at ( i ) to the end condition which will give j = 4; j < 8; j++ 您必须将( i )处的位置加到最终条件,将得出j = 4; j < 8; j++ j = 4; j < 8; j++ j = 4; j < 8; j++ and correctly loop over Eric , then return to the main loop. j = 4; j < 8; j++并正确循环遍历Eric ,然后返回主循环。

At the end hits should look like ['E','r','i','c','E','r','i','c','E','r','i','c'] in this case. 最后, hits应该看起来像['E','r','i','c','E','r','i','c','E','r','i','c']在这种情况下。

The "exercise" is a steaming load of post-refined matter. “运动”是后精制物质的蒸腾负荷。 Here is a "fixed exercise" that should hopefully make more sense, complete with an inline explanation. 这是一个“固定练习”,希望能更有意义,并附带内联说明。 (And if this is understood, the "example" nonsense should at least be a bit more clear.) (如果可以理解,那么“示例”废话至少应该更清楚一点。)

var text = "Blah blah ELEPHANT blah blah blah Eric \
            blah blah blah Eric blah blah Eric blah blah \
            blah blah blah EGRET blah Eric";

var name = "Eric";
var foundAt = [];

// Loop through the text one character at a time over the
// indices [0, text.length), incrementing i by at the end of each loop.
for(var i = 0; i < text.length; i++) {
  // And for every index in the text, start to loop over the indices in
  // our name, or from [0, name.length). Once again, the increment to
  // j happens at the end of each loop.
  for (var j = 0; j < name.length; j++) {
      // Look at where we currently are in the text, and compare the
      // character where we are currently looking in our name.
      // (Note that we look at character i+j into the text, because we
      // look 0+j into the name.)
      if (text[i + j] == name[j]) {
         // Yup, still matches our name!
         if (j == name.length - 1) {
            // The last character matched too. This must be our name, or
            // our name was found as part of a longer word.
            // Let's record the index in the text where our name started!
            foundAt.push(i);
         }
      } else {
         // Did not match our name, so we stop looking through the rest
         // of the name and continue looking through the next character
         // in the text (this terminates the inner loop early).
         break;
      }
  }
}

if (foundAt.length) {
    // \o/ Hooray!! \o/
    // We found our name at least once, show where it was found.
    console.log(foundAt);
} else {
    console.log("Nope, nothing here");
}

And a fiddle . 和一个小提琴 Try changing the name. 尝试更改名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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