Your loop is never finished.
In your script, i=10 of for(var i=1; i=10; i++) has to be condition. But i=10 is that it substitutes 10 for i. Therefore, when for(var i=1; i=10; i++) is run:
- At 1st loop, it substitutes
10 for i. i becomes 10.
- 1 of the initial value is replaced to
10 by i=10.
- At 2nd loop, it adds
1 to i. i becomes 11.
- At 3rd loop, it substitutes
10 for i. i becomes 10.
- At 4th loop, it adds
1 to i. i becomes 11.
- At 5th loop, it substitutes
10 for i. i becomes 10.
Solution:
When you want to loop from 1 to 10, how about modifying to like this?
for (var i = 1; i <= 10; i++) {
// do something
}
Also, for example, when you want to loop 10 times, how about modifying to like this?
for (var i = 0; i < 10; i++) {
// do something
}
About issue:
The documentation of the Javascript for statement describes:
A for loop repeats until a specified condition evaluates to false. The JavaScript for loop is similar to the Java and C for loop. A for statement looks as follows:
for ([initialExpression]; [condition]; [incrementExpression])
statement
When a for loop executes, the following occurs:
- The initializing expression initialExpression, if any, is executed. This expression usually initializes one or more loop counters, but the syntax allows an expression of any degree of complexity. This expression can also declare variables.
- The condition expression is evaluated. If the value of condition is true, the loop statements execute. If the value of condition is false, the for loop terminates. If the condition expression is omitted entirely, the condition is assumed to be true.
- The statement executes. To execute multiple statements, use a block statement ({ ... }) to group those statements.
If present, the update expression incrementExpression is executed.
Control returns to step 2.
for(var i=1;i=10;i++)is run, in the 1st loop,iis 10. In the 2nd loop,iis 11. And in the 3rd loop,iis 10. The loop is not finished like this. So how about modifying tofor(var i=1;i<=10;i++)orfor(var i=1;i<10;i++)? - Tanaike=assigns i =1 and then assignsi = 10before your loop begins. Equality comparison operator is==NOT=,which is used for variable assignment. - TheMaster