简体   繁体   中英

Confused about the for loop in Javascript

The result of this function is: "yoyo my brother".

Why is yo added only on the first iteration, why the result isn't: "yoyo yomy yobrother"?

function myFunction() {
  var cars = ["yo", "my", "brother"];
  var i = 2;
  var len = cars.length;
  var text = "yo";

  for (; i < len; i++) { //typo, var i=0;
    text += cars[i]+ " ";
 }
};

Oh sry, I don't know how I missed that typo xD. Thanks for your help.

In your code:

  • You should set your i to 0 to start at the first
    value of the array.

  • You set another variable for the yo text to be constant for appending and make the text variable blank.

If you want to get the "yoyo yomy yobrother" result do something like this:

function myFunction() {
  var cars = ["yo", "my", "brother"];

  var len = cars.length;
  var text = "";
  var yotext = "yo";

  for (i=0; i < len; i++) {
    text += yotext +cars[i]+ " ";
 }
 console.log(text);
};

myFunction();

Fiddle

The following assumes you meant to initialize i to 0.

Because the += operator appends text to the existing string. You're doing the following:

First iteration: "yo" + "yo" + " " = "yoyo "

Second iteration: "yoyo " + "my" + " " = "yoyo my "

Third iteration: "yoyo my " + "brother" + " " = "yoyo my brother "

You typically have three parts to a for loop:

1) An initializer - gives the variable a starting point 2) A condition - will only run the loop if it is true 3) A modifier - changes the value of the variable

Therefore a typical for loop will look like this:

for (var i = 2; i < len; i++){

}

The loop will execute in this fashion:

  1. Initialize i to the value of 2.
  2. Check to see if the condition is true (2 is less than 3)
  3. It Enters the loop and adds cars[2] to text. In other words it adds 'brother' to text.
  4. This iteration of the loop is done. The i++ now adds 1 to i. i is now 3.
  5. The condition is checked. i is no longer less than len (3 is not less than 3). So the program exits from the loop.

Is there a reason you are starting the value of i at 2 and not 0?

Let's break down the code.

We have an array, cars, that contains 3 items. You then declare i , which later serves as your iterator (a counter) in the for loop. I'm going to go ahead and assume that was unintentional and you meant for the i to be inside the for loop

for (var i=0; i<len; i++)

A for loop repeats the code inside of its brackets a specified number of times. The first part of the for statement, behind the first semicolon, is the iterator. i will change on each run of the loop based on the 3rd section of the statement, in this case i++ . This means increasing i by 1. The second section of the for statement states that the loop will only repeat while i is less than len - in this case, the number of items in our array, 3.

Finally, inside the for loop there is the line text += cars[i] + ""; That's telling JavaScript to put strings together each time the for loop runs. We start with "yo" and then on each run:

i    string
-------------------------
0    "yoyo "
1    "yoyo my"
2    "yoyo my brother"

Because the code does not prepend "yo" (initial value of text ) to cars[i]+ " " , instead it prepends text the value of which is changing with the loop.

I believe you are rather confused with the += operator than with the loop.

The line text += cars[i]+ " "; is equivalent to text = text + cars[i]+ " "; .

Going through your loop the value of text will change with each iteration, like below.

- yo
0 yoyo
1 yoyo my
2 yoyo my brother

Thus, the value of text will change in the first loop iteration, and it will no longer be "yo". So, no wonder "yoyo yomy yobrother" does not work out.

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