JavaScript: What will break, and what will break it? [closed]
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
开发者_高级运维 Improve this questionPotentially dangerous code below, line 2
Repeat: POTENTIALLY DANGEROUS CODE BELOW
var history = "";
for (start = 3; start = 1000; start += 1){
if(start % 5 == 0 || start % 3 == 0)
history += start + " "; }
Okay, this is the tenth time I've put JavaScript code in that's frozen my browser. It's putting my computer in shock. Are these panic attacks going to destroy her heart? Where can I learn about all the crap that might break my computer as I continue to learn and practice JavaScript? I'm looking for an exhaustive list, only.
Your loop: for (start = 3; start = 1000; start += 1){
The second part of a for( ; ; )
loop is the condition test. The loop will continue until the second part evaluates to false. To not create an infinite loop, change your code to:
for (var start = 3; start < 1000; start += 1){
Note: start+=1
is equal to start++
. If you want a compact code, you can replace +=1
by ++
.
An overview of the three-part for
-loop, for(initialise; condition; increment)
:
initialise
- Create variables (allowed to be empty)condition
- The loop will stop once this expression evaluates to falseincrement
- This expression is executed at the end of the loop
Always check against infinite loops: Make sure that the condition is able to evaluate tofalse
.
Commonly made mistakes:
- A negative incremental expression in conjunction with a is-lower-than comparison:
i--
decreases the counter, soi<100
will always be true (unless the variablei
is initialized at 100) - A positive incremental expression in conjunction with a is-higher-than comparison.
- A non-incrementing expression:
for(var i=0,j=0; i<100; j++)
(i
doesn't increase) - A condition which is always true (such as in your case)
You just have to learn and read about it properly. Your loop condition start = 1000
will always evaluate to true
, that's why the loop never terminates (an assignment returns the value which was assigned and any other number than 0
is evaluates to true
).
The MDN JavaScript Guide is a great resource for learning JavaScript. Particular for this situation:
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 ofcondition
is true, the loop statements execute. If the value ofcondition
is false, thefor
loop terminates. If thecondition
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.- The update expression
incrementExpression
, if there is one, executes, and control returns to step 2.
As the others said, it mostly comes down to try and error.... that is a good way of learning anyway.
your conditional statement start=1000 will always return true. You cant found such fool proof's list for this, you have to learn from these mistakes on your own.
Uh - what did you wanted here ?
for (start = 3; start = 1000; start += 1)
do you wanted this ? ( from 3 to 1000 )
for (start = 3; start <= 1000; start += 1)
in first case you will stuck on 1000
精彩评论